| 
 | 1 | +using System;  | 
 | 2 | +using System.Runtime.InteropServices;  | 
 | 3 | + | 
 | 4 | +namespace Unnatural  | 
 | 5 | +{  | 
 | 6 | +    using ConnectionStateEnum = Interop.UwConnectionStateEnum;  | 
 | 7 | +    using GameStateEnum = Interop.UwGameStateEnum;  | 
 | 8 | +    using MapStateEnum = Interop.UwMapStateEnum;  | 
 | 9 | +    using ShootingData = Interop.UwShootingData;  | 
 | 10 | +    using ShootingArray = Interop.UwShootingArray;  | 
 | 11 | +    using ExplosionData = Interop.UwExplosionData;  | 
 | 12 | +    using ExplosionsArray = Interop.UwExplosionsArray;  | 
 | 13 | +    using ChatTargetFLags = Interop.UwChatTargetFlags;  | 
 | 14 | + | 
 | 15 | +    public class ChatMessage  | 
 | 16 | +    {  | 
 | 17 | +        public string Message;  | 
 | 18 | +        public uint Sender;  | 
 | 19 | +        public ChatTargetFLags Flags;  | 
 | 20 | +    }  | 
 | 21 | + | 
 | 22 | +    public static class Events  | 
 | 23 | +    {  | 
 | 24 | + | 
 | 25 | +        public static event EventHandler<ConnectionStateEnum> ConnectionStateChanged;  | 
 | 26 | +        public static event EventHandler<GameStateEnum> GameStateChanged;  | 
 | 27 | +        public static event EventHandler<MapStateEnum> MapStateChanged;  | 
 | 28 | +        public static event EventHandler<bool> Updating;  | 
 | 29 | +        public static event EventHandler<ShootingData[]> Shooting;  | 
 | 30 | +        public static event EventHandler<ExplosionData[]> Explosions;  | 
 | 31 | +        public static event EventHandler<uint> ForceEliminated;  | 
 | 32 | +        public static event EventHandler<ChatMessage> ChatReceived;  | 
 | 33 | + | 
 | 34 | +        static readonly Interop.UwExceptionCallbackType ExceptionDelegate = new Interop.UwExceptionCallbackType(ExceptionCallback);  | 
 | 35 | +        static readonly Interop.UwConnectionStateCallbackType ConnectionStateDelegate = new Interop.UwConnectionStateCallbackType(ConnectionStateCallback);  | 
 | 36 | +        static readonly Interop.UwGameStateCallbackType GameStateDelegate = new Interop.UwGameStateCallbackType(GameStateCallback);  | 
 | 37 | +        static readonly Interop.UwMapStateCallbackType MapStateDelegate = new Interop.UwMapStateCallbackType(MapStateCallback);  | 
 | 38 | +        static readonly Interop.UwUpdateCallbackType UpdateDelegate = new Interop.UwUpdateCallbackType(UpdateCallback);  | 
 | 39 | +        static readonly Interop.UwShootingCallbackType ShootingDelegate = new Interop.UwShootingCallbackType(ShootingCallback);  | 
 | 40 | +        static readonly Interop.UwExplosionsCallbackType ExplosionsDelegate = new Interop.UwExplosionsCallbackType(ExplosionsCallback);  | 
 | 41 | +        static readonly Interop.UwForceEliminatedCallbackType ForceEliminatedDelegate = new Interop.UwForceEliminatedCallbackType(ForceEliminatedCallback);  | 
 | 42 | +        static readonly Interop.UwChatCallbackType ChatDelegate = new Interop.UwChatCallbackType(ChatCallback);  | 
 | 43 | + | 
 | 44 | +        static void ExceptionCallback([MarshalAs(UnmanagedType.LPStr)] string message)  | 
 | 45 | +        {  | 
 | 46 | +            Console.WriteLine("exception: " + message);  | 
 | 47 | +            if (System.Diagnostics.Debugger.IsAttached)  | 
 | 48 | +                System.Diagnostics.Debugger.Break();  | 
 | 49 | +        }  | 
 | 50 | + | 
 | 51 | +        static void ConnectionStateCallback(ConnectionStateEnum state)  | 
 | 52 | +        {  | 
 | 53 | +            if (ConnectionStateChanged != null)  | 
 | 54 | +                ConnectionStateChanged(null, state);  | 
 | 55 | +        }  | 
 | 56 | + | 
 | 57 | +        static void GameStateCallback(GameStateEnum state)  | 
 | 58 | +        {  | 
 | 59 | +            if (GameStateChanged != null)  | 
 | 60 | +                GameStateChanged(null, state);  | 
 | 61 | +        }  | 
 | 62 | + | 
 | 63 | +        static void MapStateCallback(MapStateEnum state)  | 
 | 64 | +        {  | 
 | 65 | +            if (MapStateChanged != null)  | 
 | 66 | +                MapStateChanged(null, state);  | 
 | 67 | +        }  | 
 | 68 | + | 
 | 69 | +        static void UpdateCallback(bool stepping)  | 
 | 70 | +        {  | 
 | 71 | +            if (Updating != null)  | 
 | 72 | +                Updating(null, stepping);  | 
 | 73 | +        }  | 
 | 74 | + | 
 | 75 | +        static void ShootingCallback(ref ShootingArray data)  | 
 | 76 | +        {  | 
 | 77 | +            if (Shooting == null)  | 
 | 78 | +                return;  | 
 | 79 | +            ShootingData[] arr = new ShootingData[data.count];  | 
 | 80 | +            int size = Marshal.SizeOf(typeof(ShootingData));  | 
 | 81 | +            for (int i = 0; i < data.count; i++)  | 
 | 82 | +            {  | 
 | 83 | +                IntPtr currentPtr = IntPtr.Add(data.data, i * size);  | 
 | 84 | +                arr[i] = Marshal.PtrToStructure<ShootingData>(currentPtr);  | 
 | 85 | +            }  | 
 | 86 | +            Shooting(null, arr);  | 
 | 87 | +        }  | 
 | 88 | + | 
 | 89 | +        static void ExplosionsCallback(ref ExplosionsArray data)  | 
 | 90 | +        {  | 
 | 91 | +            if (Explosions == null)  | 
 | 92 | +                return;  | 
 | 93 | +            ExplosionData[] arr = new ExplosionData[data.count];  | 
 | 94 | +            int size = Marshal.SizeOf(typeof(ExplosionData));  | 
 | 95 | +            for (int i = 0; i < data.count; i++)  | 
 | 96 | +            {  | 
 | 97 | +                IntPtr currentPtr = IntPtr.Add(data.data, i * size);  | 
 | 98 | +                arr[i] = Marshal.PtrToStructure<ExplosionData>(currentPtr);  | 
 | 99 | +            }  | 
 | 100 | +            Explosions(null, arr);  | 
 | 101 | +        }  | 
 | 102 | + | 
 | 103 | +        static void ForceEliminatedCallback(uint force)  | 
 | 104 | +        {  | 
 | 105 | +            if (ForceEliminated == null)  | 
 | 106 | +                return;  | 
 | 107 | +            ForceEliminated(null, force);  | 
 | 108 | +        }  | 
 | 109 | + | 
 | 110 | +        static void ChatCallback(string msg, uint sender, ChatTargetFLags flags)  | 
 | 111 | +        {  | 
 | 112 | +            if (ChatReceived == null)  | 
 | 113 | +                return;  | 
 | 114 | +            ChatMessage c = new ChatMessage();  | 
 | 115 | +            c.Message = msg;  | 
 | 116 | +            c.Sender = sender;  | 
 | 117 | +            c.Flags = flags;  | 
 | 118 | +            ChatReceived(null, c);  | 
 | 119 | +        }  | 
 | 120 | + | 
 | 121 | + | 
 | 122 | +        static Events()  | 
 | 123 | +        {  | 
 | 124 | +            AppDomain.CurrentDomain.ProcessExit += Destructor;  | 
 | 125 | +            Interop.uwInitialize(Interop.UW_VERSION);  | 
 | 126 | +            Interop.uwInitializeConsoleLogger();  | 
 | 127 | + | 
 | 128 | +            Interop.uwSetExceptionCallback(ExceptionDelegate);  | 
 | 129 | +            Interop.uwSetConnectionStateCallback(ConnectionStateDelegate);  | 
 | 130 | +            Interop.uwSetGameStateCallback(GameStateDelegate);  | 
 | 131 | +            Interop.uwSetMapStateCallback(MapStateDelegate);  | 
 | 132 | +            Interop.uwSetUpdateCallback(UpdateDelegate);  | 
 | 133 | +            Interop.uwSetShootingCallback(ShootingDelegate);  | 
 | 134 | +            Interop.uwSetExplosionsCallback(ExplosionsDelegate);  | 
 | 135 | +            Interop.uwSetForceEliminatedCallback(ForceEliminatedDelegate);  | 
 | 136 | +            Interop.uwSetChatCallback(ChatDelegate);  | 
 | 137 | + | 
 | 138 | +            // make sure that others register their callbacks too (call any method)  | 
 | 139 | +            Prototypes.All();  | 
 | 140 | +            Map.Positions();  | 
 | 141 | +            World.Entities();  | 
 | 142 | +            UwapiTasks.Init();  | 
 | 143 | +        }  | 
 | 144 | + | 
 | 145 | +        static void Destructor(object sender, EventArgs e)  | 
 | 146 | +        {  | 
 | 147 | +            Interop.uwDeinitialize();  | 
 | 148 | +        }  | 
 | 149 | +    }  | 
 | 150 | + | 
 | 151 | +    public static class InteropHelpers  | 
 | 152 | +    {  | 
 | 153 | +        public static uint[] Ids(Interop.UwIds ids)  | 
 | 154 | +        {  | 
 | 155 | +            uint[] tmp = new uint[ids.count];  | 
 | 156 | +            if (ids.count > 0)  | 
 | 157 | +                Marshal.Copy(ids.ids, (int[])(object)tmp, 0, (int)ids.count);  | 
 | 158 | +            return tmp;  | 
 | 159 | +        }  | 
 | 160 | +    }  | 
 | 161 | + | 
 | 162 | +    public static class UwapiTasks  | 
 | 163 | +    {  | 
 | 164 | +        public static ulong InsertTask(Action a)  | 
 | 165 | +        {  | 
 | 166 | +            ulong i = index++;  | 
 | 167 | +            actions.Add(i, a);  | 
 | 168 | +            return i;  | 
 | 169 | +        }  | 
 | 170 | + | 
 | 171 | +        static ulong index = 1;  | 
 | 172 | +        static System.Collections.Generic.Dictionary<ulong, Action> actions = new System.Collections.Generic.Dictionary<ulong, Action>();  | 
 | 173 | +        static readonly Interop.UwTaskCompletedCallbackType TaskCompletedDelegate = new Interop.UwTaskCompletedCallbackType(TaskCompleted);  | 
 | 174 | + | 
 | 175 | +        static void TaskCompleted(ulong taskUserData, Interop.UwTaskTypeEnum type)  | 
 | 176 | +        {  | 
 | 177 | +            if (type != Interop.UwTaskTypeEnum.None)  | 
 | 178 | +            {  | 
 | 179 | +                Action a;  | 
 | 180 | +                if (actions.TryGetValue(taskUserData, out a))  | 
 | 181 | +                    a();  | 
 | 182 | +            }  | 
 | 183 | +            actions.Remove(taskUserData);  | 
 | 184 | +        }  | 
 | 185 | + | 
 | 186 | +        static UwapiTasks()  | 
 | 187 | +        {  | 
 | 188 | +            Interop.uwSetTaskCompletedCallback(TaskCompletedDelegate);  | 
 | 189 | +        }  | 
 | 190 | + | 
 | 191 | +        public static void Init()  | 
 | 192 | +        { }  | 
 | 193 | +    }  | 
 | 194 | +}  | 
0 commit comments