NWN2-AI-Server/nwn2ai_onmoduleload/nwn2ai_onmoduleload.cs
Crom (Thibaut CHARLES) afcc468803 Binding correctly to NPC
2015-04-27 23:10:07 +02:00

169 lines
5.6 KiB
C#

//
// This script serves as a basis for new scripts. New scripts can copy this
// source file to start out.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Reflection.Emit;
using CLRScriptFramework;
using NWScript;
using NWScript.ManagedInterfaceLayer.NWScriptManagedInterface;
using NWEffect = NWScript.NWScriptEngineStructure0;
using NWEvent = NWScript.NWScriptEngineStructure1;
using NWLocation = NWScript.NWScriptEngineStructure2;
using NWTalent = NWScript.NWScriptEngineStructure3;
using NWItemProperty = NWScript.NWScriptEngineStructure4;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;
using Newtonsoft.Json;
namespace CLRScript
{
public partial class nwn2ai_onmoduleload : CLRScriptBase, ICLRScriptImplementation, IGeneratedScriptProgram
{
public nwn2ai_onmoduleload([In] NWScriptJITIntrinsics Intrinsics, [In] INWScriptProgram Host)
{
InitScript(Intrinsics, Host);
}
private nwn2ai_onmoduleload([In] nwn2ai_onmoduleload Other)
{
InitScript(Other);
LoadScriptGlobals(Other.SaveScriptGlobals());
}
//
// Include the list of types for parameters to the main function here.
// An empty list means no parameters.
//
public static Type[] ScriptParameterTypes = { };
StreamWriter debugFile;
public Int32 ScriptMain([In] object[] ScriptParameters, [In] Int32 DefaultReturnCode)
{
debugFile = File.AppendText("nwnx4\\customlog.txt");
Debug("====================================> Start");
sockBufferIn = new Queue<Tuple<string,Socket>>();
sockClients = new Dictionary<string,Socket>();
SocketConfigure();
AssignCommand(OBJECT_SELF, delegate() { ActionSpeakString("Start!", TALKVOLUME_TALK); });
DelayCommand(0.5f, Heartbeat);
return DefaultReturnCode;
}
public void Heartbeat()
{
ActionSpeakString("hb", TALKVOLUME_SHOUT);
DelayCommand(0.5f, Heartbeat);
//Process queued requests
//Debug("Proc cmds");
while(sockBufferIn.Count>0){
Tuple<string, Socket> req = sockBufferIn.Dequeue();
var socket = req.Item2;
var json = JsonConvert.DeserializeObject<dynamic>(req.Item1);
//TODO: check if json correctly parsed
switch ((string)json.action) {
case "bind": {
Debug("Bind request to '"+json.target+"'");
try {
if (!sockClients.ContainsKey((string)json.target)) {
sockClients.Add((string)json.target, socket);
socket.Send(Encoding.UTF8.GetBytes(
"{\"action\":\"bindack\", \"result\":\"ok\"}"
));
//handle env
}
else {
socket.Send(Encoding.UTF8.GetBytes(
"{\"action\":\"bindack\", \"result\":\"failed\", \"message\":\"Socket already binded with this NPC\"}"
));
}
}
catch (Exception e) {
socket.Send(Encoding.UTF8.GetBytes(
"{\"action\":\"bindack\", \"result\":\"failed\", \"message\":\"Thrown exception: "+e.ToString().Replace("\"","\\\"")+"\"}"
));
}
}break;
case "decision": {
}break;
}
}
}
void SocketConfigure()
{
sockServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 8080);
sockServer.Bind(iep);
sockServer.Listen(10);
sockServer.BeginAccept(new AsyncCallback(RequestHandler), null);
}
void RequestHandler(IAsyncResult state)
{
Socket sockClient = sockServer.EndAccept(state);
sockServer.BeginAccept(new AsyncCallback(RequestHandler), null);
sockClient.Blocking = true;
while (sockClient.Connected) {
if (sockClient.Available > 0) {
byte[] rawData = new byte[sockClient.Available];
sockClient.Receive(rawData, rawData.Length, 0);
string data = Encoding.UTF8.GetString(rawData);
Debug("Queued "+data);
sockBufferIn.Enqueue(new Tuple<string,Socket>(data, sockClient));
}
Thread.Sleep(10);
}
//Remove stored bind
var sock = sockClients.First(kvp => kvp.Value==sockClient);
sockClients.Remove(sock.Key);
}
Socket sockServer;
Queue<Tuple<string,Socket>> sockBufferIn;//Request & client
Dictionary<string, Socket> sockClients;//PNJ tag => Client socket
private void Debug(string msg){
debugFile.WriteLine(DateTime.Now.ToLongTimeString()+": "+msg);
debugFile.Flush();
}
}
}