public static void Append(this StringBuilder builder, FixedString128 fixedString)
 {
     foreach (var c in fixedString)
     {
         builder.Append((char)c.value);
     }
 }
Example #2
0
        /// <summary>
        /// Writes the string representation of a specified 64-bit floating-point number to the buffer.
        /// </summary>
        /// <param name="value">The value to write.</param>
        public void Write(double value)
        {
            FixedString128 f = default;

            f.Format((float)value);
            Write(f);
        }
        /// <summary>
        /// Writes the string representation of a specified 32-bit unsigned integer to the buffer.
        /// </summary>
        /// <param name="value">The value to write.</param>
        public void Write(uint value)
        {
            FixedString128 f = default;

            f.Append(value);
            Write(f);
        }
Example #4
0
        internal void AddOrUpdateString(FixedString128 key, TinyJsonObject value)
        {
            JsonKeyHandle jsonRefHandle;

            if (m_RootObjMap.ContainsKey(key))
            {
                jsonRefHandle = m_RootObjMap[key];
                if (JsonKeyType(jsonRefHandle) == JsonValueType.String)
                {
                    UpdateStringBufferFromObjectBasedOnVariant(value, jsonRefHandle);

                    return;
                }

                // remove the old type completely and add a new one as normal
                RemoveValueFromBuffer(jsonRefHandle);
            }

            jsonRefHandle = new JsonKeyHandle(ref this, JsonValueType.String, m_StringBuffer.Length);
            if (!value.m_StringVal.IsEmpty)
            {
                m_StringBuffer.Add(value.m_StringVal[0]);
                value.m_StringVal.Dispose();
            }
            else
            {
                m_StringBuffer.Add(value.m_Data.m_StringBuffer[value.m_Data.LocationInValueBuffer(value.m_StringRefHandle)]);
            }

            value.m_StringVal.Dispose();
            m_RootObjMap[key] = jsonRefHandle;
        }
Example #5
0
        public static unsafe bool WriteToDisk(FixedString128 gameSaveFilePath, MemoryBinaryWriter writer)
        {
            Baselib_ErrorState errorState = new Baselib_ErrorState();

            Baselib_FileIO_SyncFile gameSaveFileHandle = Baselib_FileIO_SyncOpen(gameSaveFilePath.GetUnsafePtr(), Baselib_FileIO_OpenFlags.CreateAlways | Baselib_FileIO_OpenFlags.Write, &errorState);

            if (errorState.code != Baselib_ErrorCode.Success)
            {
                return(false);
            }

            Baselib_FileIO_SyncWrite(gameSaveFileHandle, 0, (IntPtr)writer.Data, (uint)writer.Length, &errorState);
            if (errorState.code != Baselib_ErrorCode.Success)
            {
                return(false);
            }

            Baselib_FileIO_SyncFlush(gameSaveFileHandle, &errorState);
            if (errorState.code != Baselib_ErrorCode.Success)
            {
                return(false);
            }

            // If we get this far, we'll consider the write a success because it should have been completed.
            Baselib_FileIO_SyncClose(gameSaveFileHandle, &errorState);
            return(true);
        }
Example #6
0
        /**
         * Adds the object to the correct buffer associated with the provided key.
         */
        internal void AddOrUpdateField(FixedString128 key, TinyJsonObject value)
        {
            switch (value.Type)
            {
            case JsonValueType.Bool:
                AddOrUpdateBool(key, value);
                break;

            case JsonValueType.Int:
                AddOrUpdateInt(key, value);
                break;

            case JsonValueType.Float:
                AddOrUpdateFloat(key, value);
                break;

            case JsonValueType.String:
                AddOrUpdateString(key, value);
                break;

            case JsonValueType.Array:
                var tinyJsonArray = value.AsArray();
                AddOrUpdateArray(key, ref tinyJsonArray);
                break;

            case JsonValueType.Object:
                AddOrUpdateObject(key, ref value.m_Data);
                break;

            default:
                InvalidJsonTypeException();
                return;
            }
        }
            /// <summary>
            /// Writes the specified 64-bit signed integer value to the buffer with the correct formatting.
            /// </summary>
            /// <param name="value">The value to write.</param>
            public void WriteValue(long value)
            {
                FixedString128 f = default;

                f.Append(value);
                WriteValue(f);
            }
Example #8
0
        public static unsafe bool PullCompletedReadBuffer(FixedString128 gameSaveFilePath, byte *buffer, int bufferLength)
        {
            for (int i = 0; i < gameSaveReadFiles.Length; i++)
            {
                if (gameSaveReadFiles[i].filePath == gameSaveFilePath)
                {
                    Baselib_ErrorState errorState = new Baselib_ErrorState();
                    bool result = false;

                    UInt64 fileSize = Baselib_FileIO_SyncGetFileSize(gameSaveReadFiles[i].fileHandle, &errorState);
                    if ((errorState.code == Baselib_ErrorCode.Success) && ((int)fileSize == bufferLength))
                    {
                        Baselib_FileIO_SyncRead(gameSaveReadFiles[i].fileHandle, 0, (IntPtr)buffer, fileSize, &errorState);
                        if (errorState.code == Baselib_ErrorCode.Success)
                        {
                            result = true;
                        }
                    }

                    Baselib_FileIO_SyncClose(gameSaveReadFiles[i].fileHandle, &errorState);
                    gameSaveReadFiles.RemoveAtSwapBack(i);
                    return(result);
                }
            }

            return(false);
        }
 void Load(ref ClientMenuState state, ClientMenuState.Menu menu, FixedString128 name)
 {
     state.IsLoading           = true;
     state.LastLoadingMenu     = menu;
     state.LastLoadingMenuName = name;
     SceneManager.LoadScene(name.ToString(), LoadSceneMode.Additive);
 }
Example #10
0
        public unsafe bool WriteFixedString128(FixedString128 str)
        {
            int   length = (int)*((ushort *)&str) + 2;
            byte *data   = ((byte *)&str);

            return(WriteBytes(data, length));
        }
Example #11
0
        public unsafe bool WritePackedFixedString128Delta(FixedString128 str, FixedString128 baseline, NetworkCompressionModel model)
        {
            ushort length = *((ushort *)&str);
            byte * data   = ((byte *)&str) + 2;

            return(WritePackedFixedStringDelta(data, length, ((byte *)&baseline) + 2, *((ushort *)&baseline), model));
        }
Example #12
0
        internal void AddOrUpdateArray(FixedString128 key, ref TinyJsonArray source)
        {
            var newArray = AddOrReplaceArrayField(key);

            CheckNotSameDataAllocation(source.m_Data, this);
            newArray.DeepCopyArrayValues(ref source);
        }
Example #13
0
        /// <summary>
        /// Writes the string representation of a specified 64-bit unsigned integer to the buffer.
        /// </summary>
        /// <param name="value">The value to write.</param>
        public void Write(ulong value)
        {
            FixedString128 f = default;

            f.Format(value);
            Write(f);
        }
Example #14
0
 public static void IsTrue(bool b, FixedString128 s)
 {
     if (!b)
     {
         Debug.Log(FixedString.Format("<color=red>##### ASSERT =>  {0}</color>", s));
         throw new AssertException();
     }
 }
        public unsafe void PushValueField(FixedString128 key, FixedString4096 value)
        {
            AddCommaIfRequired();
            ValidateInObject();
            FixedString128 str = $"\"{key}\": \"{value}\"";

            m_Buffer.Append(str.GetUnsafePtr(), str.Length);
        }
Example #16
0
        public unsafe FixedString128 ReadPackedFixedString128Delta(FixedString128 baseline, NetworkCompressionModel model)
        {
            FixedString128 str;
            byte *         data = ((byte *)&str) + 2;

            *(ushort *)&str = ReadPackedFixedStringDelta(data, str.Capacity, ((byte *)&baseline) + 2, *((ushort *)&baseline), model);
            return(str);
        }
Example #17
0
        protected override void OnUpdate()
        {
            if (m_rlsQuery.CalculateChunkCount() > 0)
            {
                FixedString128 targetScene = new FixedString128();
                bool           isInvalid   = false;

                Entities.ForEach((ref RequestLoadScene rls) =>
                {
                    if (rls.newScene.UTF8LengthInBytes == 0)
                    {
                        return;
                    }
                    if (targetScene.Length == 0)
                    {
                        targetScene = rls.newScene;
                    }
                    else if (rls.newScene != targetScene)
                    {
                        isInvalid = true;
                    }
                }).Run();

                if (targetScene.Length > 0)
                {
                    if (isInvalid)
                    {
                        UnityEngine.Debug.LogError("Multiple scenes were requested to load during the previous frame.");
                    }
                    else
                    {
                        var curr = worldGlobalEntity.GetComponentData <CurrentScene>();
                        curr.previousScene = curr.currentScene;
                        UnityEngine.Debug.Log("Loading scene: " + targetScene);
                        SceneManager.LoadScene(targetScene.ToString());
                        latiosWorld.pauseForSceneLoad = true;
                        curr.currentScene             = targetScene;
                        curr.isSceneFirstFrame        = true;
                        worldGlobalEntity.SetComponentData(curr);
                        return;
                    }
                }
            }

            //Handle case where initial scene loads or set firstFrame to false
            var currentScene = worldGlobalEntity.GetComponentData <CurrentScene>();

            if (currentScene.currentScene.UTF8LengthInBytes == 0)
            {
                currentScene.currentScene      = SceneManager.GetActiveScene().name;
                currentScene.isSceneFirstFrame = true;
            }
            else
            {
                currentScene.isSceneFirstFrame = false;
            }
            worldGlobalEntity.SetComponentData(currentScene);
        }
        public unsafe void PushValueField(FixedString128 key, bool value)
        {
            AddCommaIfRequired();
            ValidateInObject();
            var            boolString = (value ? (FixedString128)"true" : (FixedString128)"false");
            FixedString128 str        = $"\"{key}\": {boolString}";

            m_Buffer.Append(str.GetUnsafePtr(), str.Length);
        }
        public unsafe void PushArrayField(FixedString128 key)
        {
            AddCommaIfRequired();
            ValidateInObject();
            m_Stack.Push(k_EmptyArray);
            FixedString128 str = $"\"{key}\": [";

            m_Buffer.Append(str.GetUnsafePtr(), str.Length);
        }
Example #20
0
        public void WordStorageFixedString128Works(String value)
        {
            NumberedWords  w           = new NumberedWords();
            FixedString128 fixedString = default;

            w.SetString(value);
            w.ToFixedString(ref fixedString);
            Assert.AreEqual(value, fixedString.ToString());
        }
Example #21
0
        public void RemoveField(FixedString128 key)
        {
            CheckIsCorrectType(JsonValueType.Object, Type);
            if (!m_Data.m_RootObjMap.ContainsKey(key))
            {
                ThrowKeyNotFoundException(key);
            }

            m_Data.RemoveValueFromBuffer(m_Data.m_RootObjMap[key]);
            m_Data.m_RootObjMap.Remove(key);
        }
            /// <summary>
            /// Writes the specified fixed string to the buffer as a literal.
            /// </summary>
            /// <param name="value">The string value to write.</param>
            void WriteValue(FixedString128 value)
            {
                var value_ptr    = UnsafeUtility.AddressOf(ref value);
                var value_len    = *(ushort *)value_ptr;
                var value_bytes  = (byte *)value_ptr + sizeof(ushort);
                var utf16_buffer = stackalloc char[value_len];

                // This is not actually correct -- We need Utf8ToUCS but that doesn't exist
                Unicode.Utf8ToUtf16(value_bytes, value_len, utf16_buffer, out var utf16_length, value_len);
                WriteValueLiteral(utf16_buffer, utf16_length);
            }
        public unsafe int PushValueToArray(bool value)
        {
            AddCommaIfRequired();
            ValidateInArray();
            int            index = m_Stack.Peek();
            FixedString128 str   = value ? (FixedString128)"true" : (FixedString128)"false";

            m_Buffer.Append(str.GetUnsafePtr(), str.Length);
            m_Stack.IncrementTop();
            return(index);
        }
Example #24
0
        /// <summary>
        /// Writes a copy of the specified <see cref="FixedString128"/> to the buffer.
        /// </summary>
        /// <param name="value">The value to write.</param>
        void Write(FixedString128 value)
        {
            var capacity     = value.UTF8LengthInBytes;
            var utf16_buffer = stackalloc char[capacity];

            fixed(FixedListByte128 *c = &value.AsFixedList)
            {
                Unicode.Utf8ToUtf16((byte *)c + sizeof(ushort), value.UTF8LengthInBytes, utf16_buffer, out var utf16_length, capacity);
                Write(utf16_buffer, utf16_length);
            }
        }
Example #25
0
        public void ReadWriteFixedString128()
        {
            var dataStream = new DataStreamWriter(300 * 4, Allocator.Temp);

            var src = new FixedString128("This is a string");

            dataStream.WriteFixedString128(src);

            //Assert.AreEqual(src.LengthInBytes+2, dataStream.Length);

            var reader = new DataStreamReader(dataStream.AsNativeArray());
            var dst    = reader.ReadFixedString128();

            Assert.AreEqual(src, dst);
        }
Example #26
0
        /** Creates a new, empty object field or replaces an old one, deleting its data */
        internal TinyJsonObject CreateObjectField(FixedString128 key)
        {
            // delete old object
            if (m_RootObjMap.ContainsKey(key))
            {
                var objectHandle = m_RootObjMap[key];
                RemoveValueFromBuffer(objectHandle);
            }

            // create new one
            var jsonValRef = CreateNestedObject(out var value);

            m_RootObjMap[key] = jsonValRef;
            return(value);
        }
Example #27
0
        /**
         * Creates a new, empty array associated with the key. Replaces the key if it exists.
         */
        internal TinyJsonArray AddOrReplaceArrayField(FixedString128 key)
        {
            // delete old key
            if (m_RootObjMap.ContainsKey(key))
            {
                var handle = m_RootObjMap[key];
                RemoveValueFromBuffer(handle);
            }

            // create new array
            var jsonValRef = CreateNestedArray(out var array);

            m_RootObjMap[key] = jsonValRef;
            return(array);
        }
Example #28
0
        protected override void OnUpdate()
        {
            var terminalEntity = _termQuery.GetSingletonEntity();
            var jobData        = new TerminalJobContext(this, terminalEntity, false);

            Entities
            .WithChangeFilter <UILogBuffer, HitPointsUI>()
            //.WithoutBurst()
            .ForEach((
                         ref DynamicBuffer <UILogBuffer> logBuffer,
                         in HitPointsUI hp) =>
            {
                var term = jobData.GetAccessor();

                if (term.Length == 0)
                {
                    return;
                }

                term.ClearScreen();
                term.DrawBorder();

                var emptyColor = new Color(0.05f, 0.05f, 0.05f);

                FixedString128 hpString = $"HP: {hp.current} / {hp.max}";

                int barWidth = term.Width - 20;
                int barX     = term.Width - barWidth - 1;
                int hpX      = barX - hpString.Length - 1;

                term.PrintFGColor(hpX, term.Height - 1, hpString, Color.yellow);
                term.DrawHorizontalBar(
                    term.Width - barWidth - 1,
                    term.Height - 1,
                    barWidth,
                    hp.current, hp.max,
                    Color.red, emptyColor);

                int lineCount = 0;
                for (int i = logBuffer.Length - 1;
                     i >= 0 && lineCount < term.Height - 2;
                     --i, ++lineCount)
                {
                    term.Print(2, term.Height - 2 - lineCount, logBuffer[i].Value);
                }
            }).Schedule();
        }
Example #29
0
        public static unsafe bool ReadFromDisk(FixedString128 gameSaveFilePath)
        {
            Baselib_ErrorState      errorState = new Baselib_ErrorState();
            Baselib_FileIO_SyncFile fileHandle = Baselib_FileIO_SyncOpen(gameSaveFilePath.GetUnsafePtr(), Baselib_FileIO_OpenFlags.Read, &errorState);

            if (errorState.code != Baselib_ErrorCode.Success)
            {
                return(false);
            }

            GameSaveFile gameSaveFile;

            gameSaveFile.filePath   = gameSaveFilePath;
            gameSaveFile.fileHandle = fileHandle;
            gameSaveReadFiles.Add(gameSaveFile);
            return(true);
        }
Example #30
0
        public static unsafe bool GetLength(FixedString128 gameSaveFilePath, int *length)
        {
            for (int i = 0; i < gameSaveReadFiles.Length; i++)
            {
                if (gameSaveReadFiles[i].filePath == gameSaveFilePath)
                {
                    Baselib_ErrorState errorState = new Baselib_ErrorState();
                    UInt64             fileSize   = Baselib_FileIO_SyncGetFileSize(gameSaveReadFiles[i].fileHandle, &errorState);
                    if ((errorState.code == Baselib_ErrorCode.Success) && (fileSize <= Int32.MaxValue))
                    {
                        *length = (int)fileSize;
                        return(true);
                    }
                }
            }

            return(false);
        }