Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using OpenRA.Traits;
  5.  
  6. namespace OpenRA.Mods.Rg.Traits
  7. {
  8.     public class RgUniqueIdInfo : ITraitInfo
  9.     {
  10.         public string Filename = "RgSerial.txt";
  11.  
  12.         public object Create(ActorInitializer init)
  13.         {
  14.             return new RgUniqueId(init, this);
  15.         }
  16.     }
  17.  
  18.     public class RgUniqueId : IResolveOrder
  19.     {
  20.         public string SerialFile = "";
  21.         public string Serial = "";
  22.         public const string ORDER = "RgUniqueId";
  23.         public RgUniqueId(ActorInitializer init, RgUniqueIdInfo info)
  24.         {
  25.             SerialFile = Game.SupportDir + info.Filename;
  26.  
  27.             if (!File.Exists(SerialFile))
  28.             {
  29.                 GenerateSerial();
  30.             }else
  31.             {
  32.                 LoadSerial();
  33.             }
  34.  
  35.             SendSerial(init.self);
  36.         }
  37.  
  38.         private void SendSerial(Actor self)
  39.         {
  40.             self.World.IssueOrder(new Order(ORDER, self, Serial));
  41.         }
  42.  
  43.         private void LoadSerial()
  44.         {
  45.             Serial = File.ReadAllText(SerialFile, Encoding.UTF8);
  46.         }
  47.  
  48.         private void GenerateSerial()
  49.         {
  50.             Serial = Guid.NewGuid().ToString();
  51.  
  52.             File.WriteAllText(SerialFile, Serial, Encoding.UTF8);
  53.         }
  54.  
  55.         public void ResolveOrder(Actor self, Order order)
  56.         {
  57.             if (order.OrderString == ORDER && Serial == "")
  58.             {
  59.                 Serial = order.OrderString;
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement