コード例 #1
0
        /// <summary>
        /// Read variable as struct, <remarks>ONLY work if this variable is struct</remarks> [NOT POINTER TO STRUCT]
        /// </summary>
        public void LoadStructVars()
        {
            if (!IsStruct)
            {
                throw new Exception($"{Name} not a struct.!!");
            }

            if (!JsonReflector.StructsList.ContainsKey(VarType))
            {
                throw new KeyNotFoundException($"Can't find struct When try read as {VarType}");
            }

            Struct = JsonReflector.StructsList[VarType];
        }
コード例 #2
0
        private static bool LoadStruct(string structName, bool overrideOld = false)
        {
            if (_engineJObject == null)
            {
                throw new NullReferenceException($"{_engineJObject} is null!!, call `ReadJsonEngine` first.");
            }

            if (StructsList.ContainsKey(structName) && !overrideOld)
            {
                return(true);
            }

            var structsArray = _engineJObject["structs"];

            foreach (var structObj in structsArray.Where(s => s["name"].ToString() == structName))
            {
                int offset     = 0;
                int structSize = 0;

                var ret = new JsonStruct
                {
                    StructName  = structObj["name"].ToString(),
                    StructSuper = structObj["super"].ToString()
                };

                // Get Super
                {
                    if (!string.IsNullOrWhiteSpace(ret.StructSuper))
                    {
                        // Add super struct Variables to struct first
                        if ((StructsList.ContainsKey(ret.StructSuper) && !overrideOld) || LoadStruct(ret.StructSuper, overrideOld))
                        {
                            var s = StructsList[ret.StructSuper];
                            foreach (var structVar in s.Vars)
                            {
                                var sVar = structVar.Value;
                                var jVar = new JsonVar(
                                    sVar.Name,
                                    sVar.VarType,
                                    offset,
                                    sVar.IsStruct,
                                    true);

                                ret.Vars.Add(sVar.Name, jVar);
                                offset += jVar.Size;
                            }

                            structSize += s.GetSize();
                        }
                        else
                        {
                            throw new KeyNotFoundException($"Can't find `{ret.StructSuper}` Struct.");
                        }
                    }
                }

                // Init vars
                {
                    var sVars = structObj["vars"];
                    foreach (var sVar in sVars.Children <JObject>().Properties())
                    {
                        string sName = sVar.Name.Replace("pad", $"pad_{Utils.RandomString(2)}");

                        var jVar = new JsonVar(
                            sName,
                            sVar.Value.ToString(),
                            offset,
                            IsStructType(sVar.Value.ToString()),
                            false);

                        ret.Vars.Add(sName, jVar);
                        offset += jVar.Size;

                        structSize += Utils.IsNumber(sVar.Value.ToString(), out int val)
                            ? val
                            : VarSizeFromName(sVar.Value.ToString());
                    }
                }

                // Init Struct
                ret.SetSize(structSize);

                // Add struct to struct list
                if (overrideOld)
                {
                    // Update all structs that inheritance form this overrides struct
                    // Twice to be sure all structs override
                    for (int i = 0; i < 2; i++)
                    {
                        foreach (var(sName, _) in StructsList)
                        {
                            LoadStruct(sName, true);
                        }
                    }
                }

                // check if it not in the list
                if (!StructsList.ContainsKey(ret.StructName))
                {
                    StructsList.Add(ret.StructName, ret);
                }
            }

            return(true);
        }