示例#1
0
        void Update()
        {
            var keys  = Map.Keys;
            var count = keys.Count;
            var nu    = DateTime.Now;
            var ekill = new List <string>();

            foreach (string key in keys)
            {
                var entry   = Map[key];
                int maxtime = ((entry.Modifications * 2) - count) + 1;
                int time    = (int)nu.Subtract(entry.Modified).TotalMinutes;
                if (time > maxtime || count > 5)
                {
                    Chat($"Entry {key} is over time ({time} minutes elapsed, and {maxtime} were allowed). This entry was since put in RAM modified {entry.Modifications} time(s)");
                    if (entry.Modifications > 0)
                    {
                        QuickStream.SaveString(TagFile(key), entry.ToString());
                    }
                    ekill.Add(key);
                }
            }
            foreach (string key in ekill)
            {
                Map.Kill(key);
            }
        }
示例#2
0
        public static QuickStream AppendFile(string filename, byte EndianCode = LittleEndian)
        {
            var s = File.Open(filename, FileMode.Append);
            var r = new QuickStream(s, EndianCode, true);

            //r.Position = r.Size;
            return(r);
        }
示例#3
0
 static public GINIE FromFile(string file, bool allownonexistent = true)
 {
     if (!File.Exists(file))
     {
         if (allownonexistent)
         {
             return(new GINIE());
         }
         else
         {
             return(null);
         }
     }
     // TODO: Auto-detect binary form if existent
     return(FromSource(QuickStream.LoadString(file)));
 }
        /// <summary>
        /// This function can read a GINI file.
        /// Either being a 'text' file or 'compiled' file doesn't matter
        /// this routine can autodetect that.
        /// </summary>
        /// <param name="file"></param>
        /// <param name="AcceptNonExistance"></param>
        /// <returns></returns>
        static public TGINI ReadFromFile(string file, bool AcceptNonExistance = false)
        {
            if (!File.Exists(file))
            {
                if (AcceptNonExistance)
                {
                    return(new TGINI());
                }
                Console.WriteLine($"GINI file {file} doesn't exist");
                return(null);
            }
            var ret = new TGINI();
            var b   = QuickStream.GetFile(file);

            return(ParseBytes(b));
        }
示例#5
0
        public void App(string key, string v)
        {
            var tag = SafeTag(key);
            var fil = TagFile(tag);
            var SW  = Map[tag];

            if (SW == null)
            {
                if (File.Exists(fil))
                {
                    SW = new SwapData(QuickStream.LoadString(fil));
                }
                else
                {
                    SW = new SwapData("");
                }
                Map[tag] = SW;
            }
            SW.App(v);
            Update();
        }
示例#6
0
 public string this[string key] {
     get {
         var tag = SafeTag(key);
         var fil = TagFile(tag);
         var SW  = Map[tag];
         MD();
         if (SW == null)
         {
             if (!File.Exists(fil))
             {
                 return("");
             }
             SW       = new SwapData(QuickStream.LoadString(fil));
             Map[tag] = SW;
         }
         Update();
         return(SW.ToString());
     }
     set {
         var tag = SafeTag(key);
         var fil = TagFile(tag);
         var SW  = Map[tag];
         MD();
         if (SW == null)
         {
             Chat($"Creating new entry: {tag}");
             Map[tag] = new SwapData(value, true);
         }
         else
         {
             Chat($"Rewrite existing entry: {tag}");
             SW.Def(value);
         }
         Update();
     }
 }
        /// <summary>Save as a source file (editable in text editors)</summary>

        /// <seealso cref="ToSource"/>

        public void SaveSource(string filename)
        {
            var src = ToSource();

            QuickStream.SaveString(filename, src);
        }
        }                 //End Function

        public void ReadFromBytes(byte[] b)
        {
            // This is a new approach for GINI.

            // The BlitzMax variant doesn't even support it.

            // This has not yet been tested as there is no writer for it yet.

            // I just need to find time to fully complete this experiment XD

            //g.init1st()



            var bt = new QuickStream(new MemoryStream(b)); //bytes.NewReader(b)

            var head = bt.ReadString(5);

            if (head != "GINI\x1a")
            {
                throw new Exception("The buffer read is not a GINI binary");
            }

            while (true)
            {
                var tag = bt.ReadByte();

                switch (tag)
                {
                case 1:     // Basic Variable

                    var k = bt.ReadString();

                    var v = bt.ReadString();

                    D(k, v);

                    break;

                case 2:     // Create list

                    var cklst = bt.ReadString();

                    CL(cklst, false);

                    break;

                case 3:     // Add to list

                    var kl = bt.ReadString();

                    var kv = bt.ReadString();

                    Add(kl, kv);

                    break;

                case 4:     // link list

                    var list2link = bt.ReadString();

                    var list2link2 = bt.ReadString();

                    list2link = list2link2;

                    break;

                case 255:

                    bt.Close();

                    return;

                default:

                    throw new Exception("ERROR! Unknown tag: {tag}");
                }
            } // infinite loop
        }     // func
示例#9
0
 public static byte[] GetFile(string filename)
 {
     d(); return(QuickStream.GetFile(filename));
 }
示例#10
0
        static public byte[] Pack(byte[] ibuf)
        {
            P($"Request to pack {ibuf.Length}");
            if (ibuf.Length < 1024)
            {
                return(cpy(ibuf));
            }
            var  d64  = new List <long>();
            var  d32  = new List <int>();
            var  d16  = new List <short>();
            var  bank = new BlitzBank(ibuf, BlitzEndian.Little, true);
            byte mod64;
            byte mod32;
            byte mod16;

            // 64 bit
            P("Creating Dictionary for 64 bit");
            for (int i = 0; i < ibuf.Length - 8; i += 8)
            {
                var p = bank.PeekLong(i);
                if (!d64.Contains(p))
                {
                    d64.Add(p);
                }
            }
            mod64 = (byte)(ibuf.Length % 8);
            P($"64 bit result (Entries {d64.Count}; Full: {ibuf.Length} Div: {(int)(ibuf.Length / 8)}; Mod: {mod64}; Pb: {needbit(d64.Count)} ");

            // 32 bit
            P("Creating Dictionary for 32 bit");
            for (int i = 0; i < ibuf.Length - 4; i += 4)
            {
                var p = bank.PeekInt(i);
                if (!d32.Contains(p))
                {
                    d32.Add(p);
                }
            }
            mod32 = (byte)(ibuf.Length % 4);
            P($"32 bit result (Entries {d32.Count}; Full: {ibuf.Length} Div: {(int)(ibuf.Length / 4)}; Mod: {mod32}; Pb: {needbit(d32.Count)} ");


            // 16 bit
            P("Creating Dictionary for 16 bit");
            for (int i = 0; i < ibuf.Length - 2; i += 2)
            {
                var p = bank.PeekShort(i);
                if (!d16.Contains(p))
                {
                    d16.Add(p);
                }
            }
            mod16 = (byte)(ibuf.Length % 2);
            P($"16 bit result (Entries {d16.Count}; Full: {ibuf.Length} Div: {(int)(ibuf.Length / 2)}; Mod: {mod16}; Pb: {needbit(d16.Count)} ");

            byte ubit = 64;
            byte pbit = 64;
            byte uinc = 8;
            //byte pinc = 8;
            int  dsiz  = 0;
            var  rat64 = Ratio(d64.Count, ibuf.Length / 8);
            var  rat32 = Ratio(d32.Count, ibuf.Length / 4);
            var  rat16 = Ratio(d16.Count, ibuf.Length / 2);
            byte rest  = 0;

            P($"Ratio   64:{rat64}; 32:{rat32}; 16:{rat16}");

            if (rat16 < rat64 && rat16 < rat32 && needbit(d16.Count) < 16)
            {
                ubit = 16; pbit = needbit(d16.Count); dsiz = (int)d16.Count; rest = mod16; uinc = 2;
            }
            else if (rat32 < rat64 && needbit(d32.Count) < 32)
            {
                ubit = 32; pbit = needbit(d32.Count); dsiz = (int)d32.Count; rest = mod32; uinc = 4;
            }
            P($"Chosen: {ubit} => {pbit}");
            if (pbit == 64 || pbit == ubit)
            {
                P("Packing not possible, so let's get outta here!");
                return(cpy(ibuf));
            }
            var Dict          = new Dictionary <long, long>();
            var PureMemStream = new MemoryStream();
            var MS            = new QuickStream(PureMemStream);

            MS.WriteString("JXSDA\x1a", true);
            MS.WriteByte(ubit);
            MS.WriteByte(pbit);
            MS.WriteByte(rest);
            MS.WriteInt(dsiz);
            for (int i = 0; i < dsiz; i++)
            {
                switch (ubit)
                {
                case 32:
                    Dict[d32[i]] = i;
                    MS.WriteInt(d32[i]);
                    break;

                case 64:
                    Dict[d64[i]] = i;
                    MS.WriteLong(d64[i]);
                    break;

                case 16:
                    P($"Dict create {i}/{dsiz}/{d16.Count}");
                    Dict[d16[i]] = i;
                    MS.WriteLong(d16[i]);
                    break;

                default:
                    throw new Exception("Unknown bit setting in creating dictionary (bug?)");
                }
            }
            for (int p = ibuf.Length - rest; p < ibuf.Length; p++)
            {
                MS.WriteByte(ibuf[p]);
            }
            for (int p = 0; p < ibuf.Length - uinc; p += uinc)
            {
                long value;
                switch (ubit)
                {
                case 64:
                    value = bank.PeekLong(p);
                    break;

                case 32:
                    value = bank.PeekInt(p);
                    break;

                case 16:
                    value = bank.PeekShort(p);
                    break;

                default:
                    throw new Exception("Unknown bit setting in reading bytes to pack (bug?)");
                }
                switch (pbit)
                {
                case 64:
                    throw new Exception("64 bit output invalid! (bug?)");

                case 32:
                    MS.WriteInt((int)Dict[value]);
                    break;

                case 16:
                    MS.WriteShort((short)Dict[value]);
                    break;

                case 8:
                    MS.WriteByte((byte)Dict[value]);
                    break;

                default:
                    throw new Exception("Unknown bit setting in writing bytes to pack (bug?)");
                }
            }
            var ret = PureMemStream.ToArray();

            MS.Close();
            return(ret); // temp
        }
示例#11
0
 public static void SaveString(string filename, string thestring)
 {
     d(); QuickStream.SaveString(filename, thestring);
 }
示例#12
0
 public static QuickStream StreamFromBytes(byte[] buffer, byte Endian = LittleEndian)
 {
     d(); return(QuickStream.StreamFromBytes(buffer, Endian));
 }
示例#13
0
 public static QuickStream WriteFile(string filename, byte EndiancCode = LittleEndian)
 {
     d(); return(QuickStream.WriteFile(filename, EndiancCode));
 }
示例#14
0
 public static QuickStream ReadFile(string filename, byte EndianCode = LittleEndian)
 {
     d(); return(QuickStream.ReadFile(filename, EndianCode));
 }
示例#15
0
 public static void Hello()
 {
     d(); QuickStream.Hello();
 }
示例#16
0
 public void SaveSource(string file) => QuickStream.SaveString(file, ToSource());
示例#17
0
文件: Program.cs 项目: jcr6/JXSDA_CS
 static Program()
 {
     QCol.Doing("Reading", PackFile);
     startbuf = QuickStream.GetFile(PackFile);
 }
示例#18
0
 public static string LoadString(string filename)
 {
     d(); return(QuickStream.LoadString(filename));
 }
示例#19
0
        static public byte[] Unpack(byte[] iBuf)
        {
            var PureMemStream = new MemoryStream(iBuf);
            var MS            = new QuickStream(PureMemStream);

            if (MS.ReadString(6) != "JXSDA\x1a")
            {
                Fout("Data is not JXSDA packed data"); return(null);
            }
            var ubit     = MS.ReadByte();
            var pbit     = MS.ReadByte();
            var rest     = MS.ReadByte();
            var dsiz     = MS.ReadInt();
            var dict     = new Dictionary <long, long>();
            var restbank = new List <byte>();

            P($"Unpack data: ubit:{ubit}; pbit:{pbit}; rest:{rest}; dsiz:{dsiz}");
            for (int i = 0; i < dsiz; ++i)
            {
                switch (ubit)
                {
                case 64:
                    dict[i] = MS.ReadLong();
                    break;

                case 32:
                    dict[i] = MS.ReadInt();
                    break;

                case 16:
                    dict[i] = MS.ReadShort();
                    break;

                default:
                    Fout($"Invalid ubit value {ubit} while reading dictionary");
                    return(null);
                }
            }
            for (byte i = 0; i < rest; i++)
            {
                restbank.Add(MS.ReadByte());
            }
            var UPWrite = new MemoryStream();
            var U       = new QuickStream(UPWrite);

            while (!MS.EOF)
            {
                try {
                    long value = 0;
                    switch (pbit)
                    {
                    case 32:
                        value = MS.ReadInt();
                        break;

                    case 16:
                        value = MS.ReadShort();
                        break;

                    case 8:
                        value = MS.ReadByte();
                        break;

                    default:
                        Fout($"Invalud pbit value {pbit} in reading bytes to unpack");
                        break;
                    }
                    if (!dict.ContainsKey(value))
                    {
                        Fout($"Undefined dictionary index {value}/{value:X2}");
                        return(null);
                    }
                    switch (ubit)
                    {
                    case 64:
                        U.WriteLong(dict[value]);
                        break;

                    case 32:
                        U.WriteInt((int)dict[value]);
                        break;

                    case 16:
                        U.WriteShort((short)dict[value]);
                        break;

                    default:
                        Fout($"Invalud ubit value ({ubit}) in writing bytes in unpacking");
                        return(null);
                    }
                } catch (Exception e) {
                    Fout($".NET error unpacking: {e.Message}");
                    return(null);
                }
            }
            foreach (byte b in restbank)
            {
                U.WriteByte(b);
            }
            var result = UPWrite.ToArray();

            MS.Close();
            U.Close();
            return(result); // temp
        }
示例#20
0
 public static Dictionary <string, string> LoadStringMap(string filename)
 {
     d(); return(QuickStream.LoadStringMap(filename));
 }