Advertisement
TKGP

Event Flag Reference Implementation C#

Apr 11th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1. // Replace calls to dsInterface with your own library
  2.  
  3. private const int eventFlagPtr = 0x137D7D4;
  4.  
  5. private static Dictionary<string, int> eventFlagGroups = new Dictionary<string, int>()
  6. {
  7.     {"0", 0x00000},
  8.     {"1", 0x00500},
  9.     {"5", 0x05F00},
  10.     {"6", 0x0B900},
  11.     {"7", 0x11300},
  12. };
  13.  
  14. private static Dictionary<string, int> eventFlagAreas = new Dictionary<string, int>()
  15. {
  16.     {"000", 00},
  17.     {"100", 01},
  18.     {"101", 02},
  19.     {"102", 03},
  20.     {"110", 04},
  21.     {"120", 05},
  22.     {"121", 06},
  23.     {"130", 07},
  24.     {"131", 08},
  25.     {"132", 09},
  26.     {"140", 10},
  27.     {"141", 11},
  28.     {"150", 12},
  29.     {"151", 13},
  30.     {"160", 14},
  31.     {"170", 15},
  32.     {"180", 16},
  33.     {"181", 17},
  34. };
  35.  
  36. private bool getEventFlagAddress(int ID, out int address, out uint mask)
  37. {
  38.     string idString = ID.ToString("D8");
  39.     if (idString.Length == 8)
  40.     {
  41.         string group = idString.Substring(0, 1);
  42.         string area = idString.Substring(1, 3);
  43.         int section = Int32.Parse(idString.Substring(4, 1));
  44.         int number = Int32.Parse(idString.Substring(5, 3));
  45.  
  46.         if (eventFlagGroups.ContainsKey(group) && eventFlagAreas.ContainsKey(area))
  47.         {
  48.             int offset = eventFlagGroups[group];
  49.             offset += eventFlagAreas[area] * 0x500;
  50.             offset += section * 128;
  51.             offset += (number - (number % 32)) / 8;
  52.  
  53.             address = dsInterface.ReadInt32(eventFlagPtr);
  54.             address = dsInterface.ReadInt32(address);
  55.             address += offset;
  56.  
  57.             mask = 0x80000000 >> (number % 32);
  58.             return true;
  59.         }
  60.     }
  61.  
  62.     address = 0;
  63.     mask = 0;
  64.     return false;
  65. }
  66.  
  67. public bool ReadEventFlag(int ID)
  68. {
  69.     if (getEventFlagAddress(ID, out int address, out uint mask))
  70.     {
  71.         uint flags = dsInterface.ReadUInt32(address);
  72.         return (flags & mask) != 0;
  73.     }
  74.     else
  75.         throw new ArgumentException("Unknown event flag ID: " + ID);
  76. }
  77.  
  78. public void WriteEventFlag(int ID, bool value)
  79. {
  80.     if (getEventFlagAddress(ID, out int address, out uint mask))
  81.     {
  82.         uint flags = dsInterface.ReadUInt32(address);
  83.         if (value)
  84.             flags |= mask;
  85.         else
  86.             flags &= ~mask;
  87.         dsInterface.WriteUInt32(address, flags);
  88.     }
  89.     else
  90.         throw new ArgumentException("Unknown event flag ID: " + ID);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement