NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;

public static class CallbackHandler
{
private static List<CallbackResponse> _pendingResponses = new List<CallbackResponse>();
private static List<GamePacketType> _pendingResponseType = new List<GamePacketType>();

private static List<CallbackHandlerInfo> callbackTable = new List<CallbackHandlerInfo>();

private static int lastCallbackAltitude = 0;

private static object SyncObject = new object();

//this func invoke when next game packet
public static async Task<Packet> WaitRemoteCallback(GamePacketType type)
{
_pendingResponseType.Add(type);
int startTimeOfFunction = Environment.TickCount;

CallbackResponse nextItem = null;


while (true)
{

foreach (var item in _pendingResponses)
{
if (item.Type == type)
{
nextItem = item;
break;
}
}

if(nextItem != null)
{
_pendingResponses.Remove(nextItem);
_pendingResponseType.Remove(type);

return nextItem.CallbackArg;
}

if ((startTimeOfFunction - Environment.TickCount) > 5000)
{
_pendingResponseType.Remove(type);
}

await Task.Delay(100);
}

}

#region Handler Functions
public static CallbackHandlerInfo RegisterCallback(GamePacketType callbackType, Action<Packet> callback, CallbackHandlerInfo hook, CallbackAltitude altitude)
{
CallbackState state;

int hook_alt = hook.Altitude;

if (hook_alt % 1000 == 0)
{
state = CallbackState.Minor;
}
else
{
state = CallbackState.Mini;
}

int self_alt = hook_alt;

int minorOffset = 10;
int miniOffset = 1;

if (altitude == CallbackAltitude.PreCallback)
{
minorOffset *= -1;
miniOffset *= -1;
}


if (state == CallbackState.Minor) //if its major then it will be minor
{
self_alt = self_alt + minorOffset;
}
else if (state == CallbackState.Mini) //if its minor then it will be mini
{
self_alt = self_alt + miniOffset;
}

bool altitude_is_exist = callbackTable.Any(x => x.Altitude == self_alt);


return RegisterCallback(callbackType, callback, self_alt);
}
//Major
public static CallbackHandlerInfo RegisterCallback(GamePacketType callbackType, Action<Packet> callback, CallbackAltitude altitude)
{
lastCallbackAltitude += 500;
CallbackHandlerInfo ret_val = RegisterCallback(callbackType, callback, lastCallbackAltitude);
ret_val.State = CallbackState.Major;
return ret_val;
}
public static CallbackHandlerInfo RegisterCallback(GamePacketType callbackType, Action<Packet> callback, int altitude)
{
CallbackHandlerInfo createdInfo = new CallbackHandlerInfo() { PendingType = callbackType, CallbackFunction = callback, Altitude = altitude };
callbackTable.Add(createdInfo);
callbackTable.Sort((x, y) => x.Altitude.CompareTo(y.Altitude));
return createdInfo;
}
#endregion
public static void InvokeCallback(GamePacketType callbackType, Packet args, bool singleCallback = false)
{
if (IsTypePending(callbackType))
AddResponseQueue(args, callbackType);


for (int i = 0; i < callbackTable.Count; i++)
{
CallbackHandlerInfo selectedInfo = callbackTable[i];
if (selectedInfo.PendingType == callbackType)
{
selectedInfo.CallbackFunction(args);
}
}
}
private static void AddResponseQueue(Packet response_packet, GamePacketType type)
{
CallbackResponse incoming_response = new CallbackResponse();
incoming_response.Type = type;
incoming_response.CallbackArg = response_packet;

lock (_pendingResponses)
_pendingResponses.Add(incoming_response);
}
private static bool IsTypePending(GamePacketType type)
{
foreach (var item in _pendingResponseType)
{
if (item == type)
return true;
}
return false;
}
}

public enum CallbackAltitude
{
NextCallback,
PostCallback,
PreCallback,
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.