private void OnChange(string args) { Blob blob = BlobAllocator.Blob(true); blob.ReadJson(args); string message = blob.GetString("message"); float scale = (float)blob.GetDouble("scale"); Vector3I colorVec = blob.FetchBlob("color").GetVector3I(); Color color = new Color(colorVec.X, colorVec.Y, colorVec.Z); BmFontAlign align = (BmFontAlign)blob.GetLong("align"); Blob.Deallocate(ref blob); Blob cmd = BlobAllocator.Blob(true); cmd.SetString("action", "changeSign"); cmd.SetLong("id", this._currentSignId.Id); cmd.SetString("message", message); cmd.SetLong("color", color.PackedValue); cmd.SetDouble("scale", scale); cmd.SetLong("align", (long)align); ClientContext.OverlayController.AddCommand(cmd); this.Hide(); }
public override void Update(Timestep timestep, EntityUniverseFacade entityUniverseFacade) { if (TilePower == null) { return; } Universe = entityUniverseFacade; if (entityUniverseFacade.ReadTile(Location, TileAccessFlags.SynchronousWait, out var tile)) { var config = GameContext.TileDatabase.GetTileConfiguration(Tile); if (config.Components.Select <ChargeableComponent>().Any() && tile.Configuration.Code != "staxel.tile.Sky") { TilePower.GetPowerFromComponent(config.Components.Select <ChargeableComponent>().First()); } else { var itemBlob = BlobAllocator.Blob(true); itemBlob.SetString("kind", "staxel.item.Placer"); itemBlob.SetString("tile", Tile); var item = GameContext.ItemDatabase.SpawnItemStack(itemBlob, null); ItemEntityBuilder.SpawnDroppedItem(Entity, entityUniverseFacade, item, Entity.Physics.Position, new Vector3D(0, 1, 0), Vector3D.Zero, SpawnDroppedFlags.None); entityUniverseFacade.RemoveEntity(Entity.Id); return; } Cycle.RunCycle(RunCycle); } }
public BlobDatabase(FileStream stream, Action <string> errorLogger) { _databaseFile = stream; _database = BlobAllocator.Blob(true); _databaseFile.Seek(0L, SeekOrigin.Begin); try { using (var ms = new MemoryStream()) { _databaseFile.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); _database.LoadJsonStream(ms); } stream.Seek(0, SeekOrigin.Begin); File.WriteAllBytes(stream.Name + ".bak", stream.ReadAllBytes()); } catch { if (File.Exists(stream.Name + ".bak")) { Console.ForegroundColor = ConsoleColor.Red; errorLogger($"{stream.Name} was corrupt. Will revert to backup file"); Console.ResetColor(); using (var ms = new MemoryStream(File.ReadAllBytes(stream.Name + ".bak"))) { _database.LoadJsonStream(ms); } } } NeedsStore(); Save(); }
public void UniverseUpdateBefore(Universe universe, Timestep step) { Universe = universe; if (universe.Server) { if (ServerMainLoop == null) { ServerMainLoop = ServerContext.VillageDirector?.UniverseFacade? .GetPrivateFieldValue <ServerMainLoop>("_serverMainLoop"); } if (SettingsManager.UpdateList.Count > 0) { var blob = BlobAllocator.Blob(true); var settings = blob.FetchBlob("settings"); foreach (var item in SettingsManager.UpdateList) { settings.FetchBlob(item).MergeFrom(SettingsManager.ModsSettings[item]); } using (var ms = new MemoryStream()) { blob.Write(ms); ms.Seek(0, SeekOrigin.Begin); FxCore.MessageAllPlayers(blob.ToString()); } Blob.Deallocate(ref blob); SettingsManager.UpdateList.Clear(); } } }
/// <summary> /// Reads the JSON provided by the UI from the WebOverlayRenderer /// and parses the Layout information /// </summary> /// <param name="arg"></param> private void BindSlots(string arg) { Blob blob = BlobAllocator.Blob(true); blob.ReadJson(arg); List <BlobEntry> layout = blob.FetchList("layout"); this.Layout = new Vector2F[layout.Count]; for (int k = 0; k < layout.Count; k++) { Blob entry = layout[k].Blob(); this.Layout[k] = new Vector2F((float)entry.GetLong("left"), (float)entry.GetLong("top")); } List <BlobEntry> layoutOrigins = blob.FetchList("layoutOrigins"); this.LayoutOrigin = new Vector2F[layoutOrigins.Count]; for (int j = 0; j < layoutOrigins.Count; j++) { Blob entry2 = layoutOrigins[j].Blob(); this.LayoutOrigin[j] = new Vector2F((float)entry2.GetLong("left"), (float)entry2.GetLong("top")); } List <BlobEntry> layoutSizes = blob.FetchList("layoutSizes"); this.LayoutSizes = new Vector2F[layoutSizes.Count]; for (int i = 0; i < layoutSizes.Count; i++) { Blob entry3 = layoutSizes[i].Blob(); this.LayoutSizes[i] = new Vector2F((float)entry3.GetLong("width"), (float)entry3.GetLong("height")); } Blob.Deallocate(ref blob); }
public static bool ReceiveConsoleResponse(Blob blob) { string str1 = blob.GetString("response"); var handleMessage = false; if (str1.IsNullOrEmpty()) { return(handleMessage); } try { var settings = BlobAllocator.Blob(true); settings.ReadJson(str1); if (settings.Contains("settings")) { foreach (var entry in settings.FetchBlob("settings").KeyValueIteratable) { try { SettingsManager.UpdateSettings(entry.Key, entry.Value.Blob()); handleMessage = true; } catch { // ignore } } } } catch { // ignore } return(!handleMessage); }
public static void SetObject(this Blob blob, object obj) { var tempBlob = BlobAllocator.Blob(true); tempBlob.ReadJson(JsonConvert.SerializeObject(obj)); blob.MergeFrom(tempBlob); Blob.Deallocate(ref tempBlob); }
public Blob ToStaxelBlob() { var temp = BlobAllocator.Blob(true); temp.ReadJson(Blob.ToString()); return(temp); }
public Blob CopyBlob() { var tempBlob = BlobAllocator.Blob(true); tempBlob.AssignFrom(Blob); return(tempBlob); }
internal static void UpdateSettings(string id, Blob blob) { if (!ModsSettings.ContainsKey(id)) { ModsSettings.Add(id, BlobAllocator.Blob(true)); } ModsSettings[id].MergeFrom(blob); }
public override void Construct(Blob arguments, EntityUniverseFacade entityUniverseFacade) { _constructBlob = BlobAllocator.Blob(true); _constructBlob.MergeFrom(arguments); Entity.Physics.ForcedPosition(arguments.FetchBlob("location").GetVector3I().ToVector3D()); Owner = arguments.GetLong("owner"); NeedsStore(); }
/// <summary> /// Pass the changed control hints to the UI /// </summary> /// <param name="controlHints"></param> public void ChangeControlHints(ControlHintVerbs controlHints) { bool hasMain = !string.IsNullOrEmpty(controlHints.VerbMain); bool hasAlt = !string.IsNullOrEmpty(controlHints.VerbAlt); string hintMain = ""; string hintAlt = ""; ControlHintContext context = default(ControlHintContext); if (hasMain) { if (controlHints.Rotate) { if (ClientContext.InputSource.TryGetControlHintContext(GameLogicalButton.Next, "staxel.controlHint.itemWheel.Rotate", out context)) { hintMain = context.Class; } else if (ClientContext.InputSource.TryGetControlHintContext(GameLogicalButton.Previous, "staxel.controlHint.itemWheel.Rotate", out context)) { hintMain = context.Class; } } else if (ClientContext.InputSource.TryGetControlHintContext(GameLogicalButton.Main, "staxel.controlHint.itemWheel.Main", out context)) { hintMain = context.Class; } } if (hasAlt && ClientContext.InputSource.TryGetControlHintContext(GameLogicalButton.Alt, "staxel.controlHint.itemWheel.Alt", out context)) { hintAlt = context.Class; } string verbMain = hasMain ? ClientContext.LanguageDatabase.GetTranslationString(controlHints.VerbMain) : ""; string verbAlt = hasAlt ? ClientContext.LanguageDatabase.GetTranslationString(controlHints.VerbAlt) : ""; Quad <string, string, string, string> key = new Quad <string, string, string, string>(hintMain, hintAlt, verbMain, verbAlt); string commandString = default(string); if (!this.ControlHintJsonCache.TryGetValue(key, out commandString)) { Blob blob = BlobAllocator.Blob(true); blob.SetString("hintMain", hintMain); blob.SetString("hintAlt", hintAlt); blob.SetString("verbMain", verbMain); blob.SetString("verbAlt", verbAlt); commandString = ClientContext.WebOverlayRenderer.PrepareCallFunction("updateControlHint", blob.ToString()); this.ControlHintJsonCache.Add(key, commandString); Blob.Deallocate(ref blob); } ClientContext.WebOverlayRenderer.CallPreparedFunction(commandString); }
public static Blob GetBaseBlob(Tile tile, Vector3I location) { var blob = BlobAllocator.Blob(true); blob.SetString("tile", tile.Configuration.Code); blob.SetLong("variant", tile.Variant()); blob.FetchBlob("location").SetVector3I(location); blob.FetchBlob("velocity").SetVector3D(Vector3D.Zero); return(blob); }
public override void Construct(Blob arguments, EntityUniverseFacade entityUniverseFacade) { _despawn = arguments.GetBool("despawn", false); Location = arguments.FetchBlob("location").GetVector3I(); Configuration = GameContext.TileDatabase.GetTileConfiguration(arguments.GetString("tile")); if (_logicOwner == EntityId.NullEntityId) { var blob = BlobAllocator.Blob(true); blob.SetString("tile", Configuration.Code); blob.FetchBlob("location").SetVector3I(Location); blob.SetBool("ignoreSpawn", _despawn); var entities = new Lyst <Entity>(); entityUniverseFacade.FindAllEntitiesInRange(entities, Location.ToVector3D(), 1F, entity => { if (entity.Removed) { return(false); } if (entity.Logic is GeneratorTileEntityLogic logic) { return(Location == logic.Location); } return(false); }); var tileEntity = entities.FirstOrDefault(); if (tileEntity != default(Entity)) { _logicOwner = tileEntity.Id; } else { var components = Configuration.Components.Select <GeneratorComponent>().ToList(); if (components.Any()) { var component = components.First(); if (component.Type == "solar") { _logicOwner = ChargeableTileEntityBuilder.Spawn(Location, blob, entityUniverseFacade, SolarPanelTileEntityBuilder.KindCode).Id; } else if (component.Type == "waterMill") { _logicOwner = ChargeableTileEntityBuilder.Spawn(Location, blob, entityUniverseFacade, WaterMillTileEntityBuilder.KindCode).Id; } } } } }
public override void Construct(Blob arguments, EntityUniverseFacade entityUniverseFacade) { _constructBlob = BlobAllocator.Blob(true); _constructBlob.MergeFrom(arguments); Entity.Physics.ForcedPosition(arguments.FetchBlob("location").GetVector3I().ToVector3D() + BotComponent.TileOffset); Owner = arguments.GetString("owner"); OwnerUid = arguments.GetString("uid"); _destination = Entity.Physics.Position; NeedsStore(); }
public new static Entity Spawn(EntityUniverseFacade facade, Tile tile, Vector3I location) { var entity = new Entity(facade.AllocateNewEntityId(), false, KindCode, true); var blob = BlobAllocator.Blob(true); blob.SetString(nameof(tile), tile.Configuration.Code); blob.SetLong("variant", tile.Variant()); blob.FetchBlob(nameof(location)).SetVector3I(location); blob.FetchBlob("velocity").SetVector3D(Vector3D.Zero); entity.Construct(blob, facade); facade.AddEntity(entity); return(entity); }
internal void LoadContent(GraphicsDevice graphics) { _fonts.Clear(); _backgrounds.Clear(); _images.Clear(); foreach (var asset in GameContext.AssetBundleManager.FindByExtension("uifont")) { using (var stream = GameContext.ContentLoader.ReadStream(asset)) { stream.Seek(0L, SeekOrigin.Begin); var blob = BlobAllocator.Blob(true); blob.LoadJsonStream(stream); if (Process.GetCurrentProcess().ProcessName.Contains("Staxel.Client")) { _fonts.Add(blob.GetString("code"), ContentManager.Load <SpriteFont>(blob.GetString("xnb"))); } } } foreach (var asset in GameContext.AssetBundleManager.FindByExtension("uiBackground")) { using (var stream = GameContext.ContentLoader.ReadStream(asset)) { stream.Seek(0L, SeekOrigin.Begin); var blob = BlobAllocator.Blob(true); blob.LoadJsonStream(stream); _backgrounds.Add(blob.GetString("code"), new UiBackground(graphics, blob)); } } foreach (var asset in GameContext.AssetBundleManager.FindByExtension("uiPicture")) { using (var stream = GameContext.ContentLoader.ReadStream(asset)) { stream.Seek(0L, SeekOrigin.Begin); var blob = BlobAllocator.Blob(true); blob.LoadJsonStream(stream); var image = new UiTexture2D(context => Texture2D.FromStream(context.Graphics.GraphicsDevice, GameContext.ContentLoader.ReadStream(blob.GetString("picture")))); _images.Add(blob.GetString("code"), image); } } LoadUIContent?.Invoke(graphics); }
public override void Interact(Entity entity, EntityUniverseFacade facade, ControlState main, ControlState alt) { if (alt.DownClick) { Blob blob = BlobAllocator.Blob(true); blob.SetLong("id", this.Entity.Id.Id); blob.SetString("message", this._message); blob.SetLong("color", this._color.PackedValue); blob.SetDouble("scale", this._scale); blob.SetLong("align", (long)this._align); entity.Effects.Trigger(ShowSignInterfaceEffectBuilder.BuildTrigger(blob)); } }
public static Entity Spawn(Vector3I position, Blob config, EntityUniverseFacade universe) { var entity = new Entity(universe.AllocateNewEntityId(), false, KindCode, true); var blob = BlobAllocator.Blob(true); blob.SetString("kind", KindCode); blob.FetchBlob("position").SetVector3D(position.ToTileCenterVector3D()); blob.FetchBlob("location").SetVector3I(position); blob.FetchBlob("velocity").SetVector3D(Vector3D.Zero); blob.SetString("tile", config.GetString("tile")); entity.Construct(blob, universe); universe.AddEntity(entity); //var itemBlob = BlobAllocator.Blob(true); //itemBlob.SetString("kind", "staxel.item.Placer"); //itemBlob.SetString("tile", config.GetString("tile")); //var item = GameContext.ItemDatabase.SpawnItem(itemBlob, null); //if (item == Item.NullItem) { // return entity; //} //if (entity.Inventory == null) { // var inventory = new EntityInventory(entity); // entity.GetType().GetProperty("Inventory").GetSetMethod(true).Invoke(entity, new[] { inventory }); // var inventoryBlob = BlobAllocator.Blob(true); // inventoryBlob.SetLong("index", 0); // inventoryBlob.SetLong("swapIndex", 0); // inventoryBlob.SetLong("revision", 0); // entity.Inventory.RestoreFrom(inventoryBlob); //} //if (entity.Collections == null) { // var collections = new EntityCollections(entity); // entity.GetType().GetProperty("Collections").GetSetMethod(true).Invoke(entity, new[] { collections }); // var collectionBlob = BlobAllocator.Blob(true); // collectionBlob.SetLong("version", 2L); // collectionBlob.FetchBlob("items"); // collectionBlob.SetLong("count", 0L); // entity.Collections.RestoreFrom(collectionBlob); //} //entity.Inventory.SetActiveIndex(0); //entity.Inventory.GiveItem(new ItemStack(item, 1)); return(entity); }
public static Entity Spawn(EntityUniverseFacade facade, Tile tile, Vector3I location) { Entity entity = new Entity(facade.AllocateNewEntityId(), false, StormMakerTileStateEntityBuilder.KindCode, true); Blob blob = BlobAllocator.Blob(true); blob.SetString(nameof(tile), tile.Configuration.Code); blob.FetchBlob(nameof(location)).SetVector3I(location); blob.SetLong("variant", (long)tile.Variant()); blob.FetchBlob("position").SetVector3D(location.ToTileCenterVector3D()); blob.FetchBlob("velocity").SetVector3D(Vector3D.Zero); entity.Construct(blob, facade); Blob.Deallocate(ref blob); facade.AddEntity(entity); return(entity); }
public static Entity Spawn(EntityUniverseFacade facade, Tile tile, Vector3I location) { var spawnRecord = facade.AllocateNewEntityId(); var entity = new Entity(spawnRecord, false, KindCode, true); var blob = BlobAllocator.Blob(true); blob.SetString("tile", tile.Configuration.Code); blob.FetchBlob("location").SetVector3I(location); blob.SetLong("variant", tile.Variant()); blob.FetchBlob("position").SetVector3D(location.ToTileCenterVector3D()); entity.Construct(blob, facade); Blob.Deallocate(ref blob); facade.AddEntity(entity); return(entity); }
/// <summary> /// Setup the UI to show a specific sign's information /// </summary> /// <param name="message"></param> /// <param name="color"></param> /// <param name="scale"></param> public void Setup(EntityId entityId, string message, Color color, float scale, BmFontAlign align) { this._currentSignId = entityId; Blob blob = BlobAllocator.Blob(true); blob.SetLong("id", entityId.Id); blob.SetString("message", message); blob.FetchBlob("color").SetVector3F(new Vector3F(color.R, color.G, color.B)); blob.SetDouble("scale", scale); blob.SetLong("align", (long)align); this._data = blob.ToString(); Blob.Deallocate(ref blob); }
public void ReadFile <T>(string fileName, Action <T> onLoad, bool inputIsText = false) { new Thread(() => { if (FileExists(fileName)) { var data = File.ReadAllBytes(Path.Combine(_localContentLocation, fileName)); var stream = new MemoryStream(data, 0, data.Length, false, true); stream.Seek(0, SeekOrigin.Begin); Blob input; if (!inputIsText) { input = BlobAllocator.Blob(false); input.Read(stream); } else { if (typeof(T) == typeof(string)) { onLoad((T)(object)stream.ReadAllText()); return; } input = BlobAllocator.AcquireAllocator().NewBlob(false); input.LoadJsonStream(stream); } stream.Close(); stream.Dispose(); if (typeof(T) == input.GetType()) { onLoad((T)(object)input); return; } using (var json = new MemoryStream()) { input.SaveJsonStream(json); json.Seek(0L, SeekOrigin.Begin); onLoad(JsonConvert.DeserializeObject <T>(json.ReadAllText())); Blob.Deallocate(ref input); } return; } onLoad(default);
public override void Construct(Blob arguments, EntityUniverseFacade entityUniverseFacade) { Location = arguments.FetchBlob("location").GetVector3I(); Configuration = GameContext.TileDatabase.GetTileConfiguration(arguments.GetString("tile")); if (_logicOwner == EntityId.NullEntityId) { if (Configuration.Components.Contains <SwitchTileComponent>()) { var components = Configuration.Components.Get <SwitchTileComponent>(); On = GameContext.TileDatabase.GetTileConfiguration(components.On); Off = GameContext.TileDatabase.GetTileConfiguration(components.Off); } var blob = BlobAllocator.Blob(true); blob.SetString("tile", Configuration.Code); blob.FetchBlob("location").SetVector3I(Location); var entities = new Lyst <Entity>(); entityUniverseFacade.FindAllEntitiesInRange(entities, Location.ToVector3D(), 1F, entity => { if (entity.Removed) { return(false); } if (entity.Logic is SwitchTileEntityLogic logic) { return(Location == logic.Location); } return(false); }); var tileEntity = entities.FirstOrDefault(); if (tileEntity != default(Entity)) { _logicOwner = tileEntity.Id; } else { _logicOwner = SwitchTileEntityBuilder.Spawn(Location, blob, entityUniverseFacade).Id; } } }
public static Item MakeItem(this TileConfiguration tile, string kind = "staxel.item.placer") { var itemBlob = BlobAllocator.Blob(true); itemBlob.SetString("kind", kind); itemBlob.SetString("tile", tile.Code); var item = GameContext.ItemDatabase.SpawnItemStack(itemBlob, null); Blob.Deallocate(ref itemBlob); if (item.IsNull()) { return(Item.NullItem); } return(item.Item); }
internal static void AfterLoad(PlayerEntityLogic __instance) { if (__instance == null) { return; } foreach (var modInstance in GameContext.ModdingController.GetPrivateFieldValue <IEnumerable>("_modHooks")) { if (modInstance.GetPrivateFieldValue <object>("_instance") is IFoxModHook mod) { var blob = BlobAllocator.Blob(true); blob.AssignFrom(__instance.PlayerEntity.Blob); mod.OnPlayerLoadAfter(blob); Blob.Deallocate(ref blob); } } }
public static Entity Spawn(Vector3I position, EntityUniverseFacade universe, Entity owner) { var entity = new Entity(universe.AllocateNewEntityId(), false, KindCode, true); var blob = BlobAllocator.Blob(true); blob.SetString("kind", KindCode); blob.FetchBlob("position").SetVector3D(position.ToTileCenterVector3D()); blob.FetchBlob("location").SetVector3I(position); blob.SetLong("owner", owner.Id.Id); entity.Construct(blob, universe); universe.AddEntity(entity); return(entity); }
internal static void Reload() { _tiles = new Dictionary <string, string>(); _items = new Dictionary <string, string>(); foreach (var file in GameContext.AssetBundleManager.FindByExtension(".tile.override")) { var stream = GameContext.ContentLoader.ReadStream(file); var blob = BlobAllocator.Blob(true); blob.LoadJsonStream(stream); stream.Close(); stream.Dispose(); if (blob.Contains("__inherits") && blob.GetString("__inherits").EndsWith(".tile")) { _tiles.Add(blob.GetString("__inherits"), file); } Blob.Deallocate(ref blob); } foreach (var file in GameContext.AssetBundleManager.FindByExtension(".item.override")) { var stream = GameContext.ContentLoader.ReadStream(file); var blob = BlobAllocator.Blob(true); blob.LoadJsonStream(stream); stream.Close(); stream.Dispose(); if (blob.Contains("__inherits") && blob.GetString("__inherits").EndsWith(".item")) { _items.Add(blob.GetString("__inherits"), file); } Blob.Deallocate(ref blob); } }
public static Entity Spawn(Vector3I position, EntityUniverseFacade universe, string owner, string uid, string bot = "nimbusfox.worldedit.entity.bot") { var entity = new Entity(universe.AllocateNewEntityId(), false, bot, true); var blob = BlobAllocator.Blob(true); blob.SetString("kind", bot); blob.FetchBlob("position").SetVector3D(position.ToTileCenterVector3D()); blob.FetchBlob("location").SetVector3I(position); blob.SetString("owner", owner); blob.SetString("uid", uid); entity.Construct(blob, universe); universe.AddEntity(entity); return(entity); }
public static Entity Spawn(Vector3I position, Blob config, EntityUniverseFacade universe) { var entity = new Entity(universe.AllocateNewEntityId(), false, KindCode, true); var blob = BlobAllocator.Blob(true); blob.SetString("kind", KindCode); blob.FetchBlob("position").SetVector3D(position.ToTileCenterVector3D()); blob.FetchBlob("location").SetVector3I(position); blob.FetchBlob("velocity").SetVector3D(Vector3D.Zero); blob.FetchBlob("config").MergeFrom(config); entity.Construct(blob, universe); universe.AddEntity(entity); return(entity); }