public IEnumerator Load(World.Data world) { ActionProgressBar.UpdateProgress("Loading Map Prefabs", 0f); yield return(null); yield return(null); Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < world.prefabData.Count; i++) { if (sw.Elapsed.TotalSeconds > 1f || i == 0 || i == world.prefabData.Count - 1) { ActionProgressBar.UpdateProgress("Loading Map Prefabs", (float)world.prefabData.Count / (float)i); yield return(null); yield return(null); sw.Reset(); } ProtoBuf.PrefabData prefabData = world.prefabData[i]; CreatePrefab(StringPool.Get(prefabData.id), prefabData.category, prefabData.position, prefabData.rotation, prefabData.scale); } sw.Stop(); }
private void ScaleDealtDamage(HitInfo hitInfo) { string bodypart = StringPool.Get(hitInfo.HitBone) ?? string.Empty; var ammoMod = 1.0f; var heldWeapon = hitInfo?.Weapon?.GetItem()?.GetHeldEntity()?.GetComponent <BaseProjectile>() ?? null; var ammoName = heldWeapon?.primaryMagazine?.ammoType?.shortname ?? string.Empty; if (weaponData.Weapons.ContainsKey(ammoName)) { ammoMod = weaponData.Weapons[ammoName].GlobalModifier; } string weapon = hitInfo?.Weapon?.GetItem()?.info?.shortname ?? string.Empty; if (string.IsNullOrEmpty(weapon)) { return; } if (InList(weapon, bodypart)) { var globalMod = weaponData.Weapons[weapon].GlobalModifier; var individualMod = weaponData.Weapons[weapon].IndividualParts[bodypart]; var totalMod = (globalMod + individualMod + ammoMod) - 2; if (totalMod != 1.0f) { hitInfo.damageTypes.ScaleAll(totalMod); } } else if (ammoMod != 1.0f) { hitInfo.damageTypes.ScaleAll(ammoMod); } }
public SkillState(Dictionary <string, AbstractStoragePool> poolByName, StringPool strings, StringType stringType, Annotation annotationType, List <AbstractStoragePool> types, FileInputStream @in, Mode mode) : base(strings, @in.Path, mode, types, poolByName, stringType, annotationType) { try { AbstractStoragePool p = null; poolByName.TryGetValue("testenum", out p); TestEnumsField = (null == p) ? (P0)Parser.newPool("testenum", null, types) : (P0)p; poolByName.TryGetValue("testenum:default", out p); Testenum_defaultsField = (null == p) ? (P1)Parser.newPool("testenum:default", TestEnumsField, types) : (P1)p; poolByName.TryGetValue("testenum:second", out p); Testenum_secondsField = (null == p) ? (P2)Parser.newPool("testenum:second", TestEnumsField, types) : (P2)p; poolByName.TryGetValue("testenum:third", out p); Testenum_thirdsField = (null == p) ? (P3)Parser.newPool("testenum:third", TestEnumsField, types) : (P3)p; poolByName.TryGetValue("testenum:last", out p); Testenum_lastsField = (null == p) ? (P4)Parser.newPool("testenum:last", TestEnumsField, types) : (P4)p; } catch (System.InvalidCastException e) { throw new ParseException(@in, -1, e, "A super type does not match the specification; see cause for details."); } foreach (AbstractStoragePool t in types) { poolByName[t.Name] = t; } finalizePools(@in); @in.close(); }
/// <summary> /// Make sure string is added to pool /// </summary> internal static void Prepare(StringPool pool, string value, int resourceId = -1) { if (value != null) { pool.Add(value, resourceId); } }
private void OnPlayerInit(BasePlayer player) { if (player.HasPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot)) { timer.Once(2, () => OnPlayerInit(player)); return; } PetData info; if (!npcSaveList.TryGetValue(player.userID, out info) || !info.NeedToSpawn) { return; } Puts("Loading pet..."); BaseEntity pet = InstantiateEntity(StringPool.Get(info.prefabID), new Vector3(info.x, info.y, info.z), new Quaternion()); if (pet == null) { return; } NPCController controller = player.gameObject.AddComponent <NPCController>(); pet.enableSaving = false; pet.Spawn(); controller.npcAi = pet.gameObject.AddComponent <NpcAI>(); controller.npcAi.owner = controller; controller.npcAi.inventory.Load(ProtoBuf.ItemContainer.Deserialize(info.inventory)); info.NeedToSpawn = false; }
public static void print_prefabs(Arg arg) { PrefabPoolCollection pool = GameManager.server.pool; if (pool.storage.Count == 0) { arg.ReplyWith("Prefab pool is empty."); return; } string @string = arg.GetString(0, string.Empty); TextTable textTable = new TextTable(); textTable.AddColumn("id"); textTable.AddColumn("name"); textTable.AddColumn("count"); foreach (KeyValuePair <uint, PrefabPool> item in pool.storage) { string text = item.Key.ToString(); string text2 = StringPool.Get(item.Key); string text3 = item.Value.Count.ToString(); if (string.IsNullOrEmpty(@string) || text2.Contains(@string, CompareOptions.IgnoreCase)) { textTable.AddRow(text, Path.GetFileNameWithoutExtension(text2), text3); } } arg.ReplyWith(textTable.ToString()); }
public override void Hurt(HitInfo info) { bool flag = false; if ((double)info.damageTypes.Total() >= (double)this.health) { this.health = 1000000f; this.myAI.CriticalDamage(); flag = true; } base.Hurt(info); if (flag) { return; } foreach (BaseHelicopter.weakspot weakspot in this.weakspots) { foreach (string bonename in weakspot.bonenames) { if ((int)info.HitBone == (int)StringPool.Get(bonename)) { weakspot.Hurt(info.damageTypes.Total(), info); this.myAI.WeakspotDamaged(weakspot, info); } } } }
public void SpawnVisual(BasePlayer player) { if (_visualEntity != null || !Configuration.ShowOnBack) { return; } BaseEntity entity = GameManager.server.CreateEntity(BackpackPrefab, new Vector3(0, -0.1f, 0), Quaternion.Euler(-5, -90, 180)); DroppedItemContainer container = entity as DroppedItemContainer; container.inventory = new ItemContainer(); container.inventory.ServerInitialize(null, 0); container.inventory.GiveUID(); container.inventory.entityOwner = container; container.inventory.SetFlag(ItemContainer.Flag.NoItemInput, true); container.CancelInvoke(container.RemoveMe); var visualBackpack = entity.gameObject.AddComponent <VisualBackpack>(); visualBackpack.Backpack = this; entity.limitNetworking = true; entity.SetParent(player, StringPool.Get(BackBone)); entity.SetFlag(BaseEntity.Flags.Locked, true); entity.Spawn(); entity.name = "backpack"; _visualEntity = entity; }
private void Init() { lang.RegisterMessages(new Dictionary <string, string> { { "NotEnoughItems", "Not enough {0}! You need {1} but you only have {2}" }, { "NotAllowed", "<color=red>You are not allowed to use that command!</color>" }, { "UnknownGrade", "<color=red>Unknown grade!</color>" }, { "UnknownCategory", "<color=red>Unknown category!</color>" }, { "NotLookingAt", "<color=red>You are not looking at a building block!</color>" }, { "FinishedUp", "Finished upgrading!" }, { "FinishedDown", "Finished downgrading!" }, { "AlreadyRunning", "Already running, please wait!" }, { "AnotherProcess", "Another process already running, please try again in a few seconds!" } }, this); configData = Config.ReadObject <ConfigData>(); foreach (var category in configData.Categories) { var data = new HashSet <uint>(); foreach (var prefab in category.Value) { var prefabId = StringPool.Get(prefab); if (prefabId <= 0) { continue; } data.Add(prefabId); } categories.Add(category.Key, data); } }
public void Load(WorldSerialization blob) { var terrainSize = new Vector3(blob.world.size, 1000, blob.world.size); var terrainPosition = 0.5f * terrainSize; Terrain land = GameObject.FindGameObjectWithTag("Land").GetComponent <Terrain>(); Terrain water = GameObject.FindGameObjectWithTag("Water").GetComponent <Terrain>(); WorldConverter.MapInfo terrains = WorldConverter.worldToTerrain(blob); land.terrainData.heightmapResolution = terrains.resolution; land.terrainData.size = terrains.size; water.terrainData.heightmapResolution = terrains.resolution; water.terrainData.size = terrains.size; land.terrainData.SetHeights(0, 0, terrains.land.heights); water.terrainData.SetHeights(0, 0, terrains.water.heights); land.terrainData.alphamapResolution = terrains.resolution; land.terrainData.size = terrains.size; land.terrainData.splatPrototypes = getTextures(); land.terrainData.SetAlphamaps(0, 0, terrains.splatMap); GameObject defaultObj = Resources.Load <GameObject>("Prefabs/DefaultPrefab"); for (int i = 0; i < terrains.prefabData.Length; i++) { Vector3 pos = new Vector3(terrains.prefabData[i].position.x, terrains.prefabData[i].position.y, terrains.prefabData[i].position.z); Vector3 scale = new Vector3(terrains.prefabData[i].scale.x, terrains.prefabData[i].scale.y, terrains.prefabData[i].scale.z); Quaternion rotation = Quaternion.Euler(new Vector3(terrains.prefabData[i].rotation.x, terrains.prefabData[i].rotation.y, terrains.prefabData[i].rotation.z)); GameObject g = FileSystem.Load <GameObject>(StringPool.Get((blob.world.prefabs[i].id))); GameObject newObject = Instantiate(g, pos + terrainPosition, rotation); newObject.transform.localScale = scale; PrefabDataHolder pdh = newObject.GetComponent <PrefabDataHolder>(); if (pdh == null) { newObject.AddComponent <PrefabDataHolder>(); } pdh.prefabData = terrains.prefabData[i]; } GameObject pathObj = Resources.Load <GameObject>("Paths/Path"); for (int i = 0; i < terrains.pathData.Length; i++) { Vector3 averageLocation = Vector3.zero; for (int j = 0; j < terrains.pathData[i].nodes.Length; j++) { averageLocation += terrains.pathData[i].nodes[j]; } averageLocation /= terrains.pathData[i].nodes.Length; GameObject newObject = Instantiate(pathObj, averageLocation + terrainPosition, Quaternion.identity); newObject.GetComponent <PathDataHolder>().pathData = terrains.pathData[i]; newObject.GetComponent <PathDataHolder>().offset = terrainPosition; } }
void OnPlayerInit(BasePlayer player) { if (player.HasPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot)) { timer.Once(2, () => OnPlayerInit(player)); return; } PetInfo info; if (!SaveNpcList.TryGetValue(player.userID, out info) || !info.NeedToSpawn) { return; } Puts("Loading pet..."); var pet = GameManager.server.CreateEntity(StringPool.Get(info.prefabID), new Vector3(info.x, info.y, info.z)); if (pet == null) { return; } var comp = player.gameObject.AddComponent <NpcControl>(); pet.Spawn(); comp.npc = pet.gameObject.AddComponent <NpcAI>(); comp.npc.owner = comp; comp.npc.inventory.Load(ProtoBuf.ItemContainer.Deserialize(info.inventory)); info.NeedToSpawn = false; }
void Init() { LoadData(); turretPrefabId = StringPool.Get(turretPrefab); permission.RegisterPermission("turretconfig.infiniteammo", this); bulletDamage = GetConfig("bulletDamage", 10f); bulletSpeed = GetConfig("bulletSpeed", 10f); ammoType = GetConfig("ammoType", "ammo.rifle"); adminOverride = GetConfig("adminOverride", true); animalOverride = GetConfig("animalOverride", false); sleepOverride = GetConfig("sleepOverride", false); animals = GetConfig <List <object> >("animals", GetPassiveAnimals()); useGlobalDamageModifier = GetConfig("useGlobalDamageModifier", false); globalDamageModifier = GetConfig("globalDamageModifier", 1f); health = GetConfig("health", 750f); aimCone = GetConfig("aimCone", 5f); sightRange = GetConfig("sightRange", 30f); infiniteAmmo = GetConfig("infiniteAmmo", false); LoadTurrets(); }
public HurtEvent(HitInfo info) { _info = info; string bonename = StringPool.Get(info.HitBone); HitBone = bonename == ""?"unknown":bonename; }
/** * read String pool, for apk binary xml file and resource table. */ public static async Task <StringPool> readStringPool(ByteBuffer buffer, StringPoolHeader stringPoolHeader) { long beginPos = buffer.position(); long[] offsets = new long[(int)stringPoolHeader.getStringCount()]; // read strings offset if (stringPoolHeader.getStringCount() > 0) { for (int idx = 0; idx < stringPoolHeader.getStringCount(); idx++) { offsets[idx] = Buffers.readUInt(buffer); } } // read flag // the string index is sorted by the string values if true bool sorted = (stringPoolHeader.getFlags() & StringPoolHeader.SORTED_FLAG) != 0; // string use utf-8 format if true, otherwise utf-16 bool utf8 = (stringPoolHeader.getFlags() & StringPoolHeader.UTF8_FLAG) != 0; // read strings. the head and metas have 28 bytes long stringPos = beginPos + stringPoolHeader.getStringsStart() - stringPoolHeader.getHeaderSize(); buffer.position((int)stringPos); StringPoolEntry[] entries = new StringPoolEntry[offsets.Length]; for (int i = 0; i < offsets.Length; i++) { entries[i] = new StringPoolEntry(i, stringPos + offsets[i]); } string lastStr = null; long lastOffset = -1; StringPool stringPool = new StringPool((int)stringPoolHeader.getStringCount()); foreach (StringPoolEntry entry in entries) { if (entry.getOffset() == lastOffset) { stringPool.set(entry.getIdx(), lastStr); continue; } buffer.position((int)entry.getOffset()); lastOffset = entry.getOffset(); string str = await ParseUtils.readString(buffer, utf8); lastStr = str; stringPool.set(entry.getIdx(), str); } // read styles if (stringPoolHeader.getStyleCount() > 0) { // now we just skip it } buffer.position((int)(beginPos + stringPoolHeader.getBodySize())); return(stringPool); }
internal string GetClass() { using (var temp = StringPool.Borrow()) { var sb = temp.Item; sb.Append("m-portlet"); if (this.Border) { sb.Append(" m-portlet--bordered"); } if (this.Semi) { sb.Append(" m-portlet--bordered-semi"); } if (this.Rounded) { sb.Append(" m-portlet--rounded"); } if (!this.Air) { sb.Append(" m-portlet--unair"); } if (this.SmallHead) { sb.Append(" m-portlet--head-sm"); } if (!string.IsNullOrEmpty(this.BgColor)) { sb.AppendFormat(" m--bg-{0}", this.BgColor); } return(sb.ToString()); } }
internal unsafe Wrapper(EngineFuncs engineFuncs, GlobalVars.Internal *pGlobals) : base(new EngineShared(engineFuncs)) { Log.Message("Initializing wrapper"); EntityDictionary = new EntityDictionary(engineFuncs); //Make sure this knows how to convert edicts EntVars.EntityDictionary = EntityDictionary; EngineFuncs = engineFuncs; Globals = new GlobalVars(pGlobals, EntityDictionary); EntityDictionary.SetGlobals(Globals); StringPool = new StringPool(Globals); EngineString.StringPool = StringPool; //Create interface implementations EngineServer = new EngineServer(EngineFuncs, StringPool, EntityDictionary, Globals, FileSystem); CVar = new CVar.CVar(EngineFuncs); }
/** * Create a new skill file based on argument path and mode. * * @throws IOException * on IO and mode related errors * @throws SkillException * on file or specification consistency errors * @note suppress unused warnings, because sometimes type declarations are * created, although nobody is using them */ public static new SkillFile open(string path, params Mode[] mode) { ActualMode actualMode = new ActualMode(mode); try { switch (actualMode.open) { case Mode.Create: // initialization order of type information has to match file // parser // and can not be done in place StringPool strings = new StringPool(null); List <AbstractStoragePool> types = new List <AbstractStoragePool>(1); StringType stringType = new StringType(strings); Annotation annotation = new Annotation(types); return(new SkillState(new Dictionary <string, AbstractStoragePool>(), strings, stringType, annotation, types, FileInputStream.open(path, false), actualMode.close)); case Mode.Read: Parser p = new Parser(FileInputStream.open(path, actualMode.close == Mode.ReadOnly)); return(p.read <SkillState>(typeof(SkillState), actualMode.close)); default: throw new System.InvalidOperationException("should never happen"); } } catch (SkillException e) { // rethrow all skill exceptions throw e; } catch (Exception e) { throw new SkillException(e); } }
public override void Read(Stream source) { uint spriteFileEntriesCount = DataStream.ReadUInt32(source); uint spriteFileEntriesPosition = DataStream.ReadUInt32(source); uint spriteEntriesCount = DataStream.ReadUInt32(source); uint spriteEntriesPosition = DataStream.ReadUInt32(source); source.Seek(spriteFileEntriesPosition, SeekOrigin.Begin); for (int i = 0; i < spriteFileEntriesCount; i++) { var entry = new SpriteFileEntry(); entry.Id = DataStream.ReadUInt32(source); entry.Name = StringPool.Read(source); entry.FileName = StringPool.Read(source); entry.Index = DataStream.ReadUInt32(source); fileEntries.Add(entry); } source.Seek(spriteEntriesPosition, SeekOrigin.Begin); for (int i = 0; i < spriteEntriesCount; i++) { var entry = new SpriteEntry(); entry.Id = DataStream.ReadUInt32(source); entry.Name = StringPool.Read(source); entry.Index = DataStream.ReadUInt16(source); entry.GroupId = DataStream.ReadUInt16(source); entries.Add(entry); } }
private GameObject Instantiate(string strPrefab, Vector3 pos, Quaternion rot) { if (!strPrefab.IsLower()) { Debug.LogWarning("Converting prefab name to lowercase: " + strPrefab); strPrefab = strPrefab.ToLower(); } GameObject gameObject = FindPrefab(strPrefab); if (!gameObject) { Debug.LogError("Couldn't find prefab \"" + strPrefab + "\""); return(null); } GameObject gameObject2 = pool.Pop(StringPool.Get(strPrefab), pos, rot); if (gameObject2 == null) { gameObject2 = Facepunch.Instantiate.GameObject(gameObject, pos, rot); gameObject2.name = strPrefab; } else { gameObject2.transform.localScale = gameObject.transform.localScale; } if (!Clientside && Serverside && gameObject2.transform.parent == null) { SceneManager.MoveGameObjectToScene(gameObject2, Rust.Server.EntityScene); } return(gameObject2); }
public static void export_prefabs(ConsoleSystem.Arg arg) { PrefabPoolCollection prefabPoolCollection = GameManager.server.pool; if (prefabPoolCollection.storage.Count == 0) { arg.ReplyWith("Prefab pool is empty."); return; } string str = arg.GetString(0, string.Empty); StringBuilder stringBuilder = new StringBuilder(); foreach (KeyValuePair <uint, PrefabPool> keyValuePair in prefabPoolCollection.storage) { string str1 = keyValuePair.Key.ToString(); string str2 = StringPool.Get(keyValuePair.Key); string str3 = keyValuePair.Value.Count.ToString(); if (!string.IsNullOrEmpty(str) && !str2.Contains(str, CompareOptions.IgnoreCase)) { continue; } stringBuilder.AppendLine(string.Format("{0},{1},{2}", str1, Path.GetFileNameWithoutExtension(str2), str3)); } File.WriteAllText("prefabs.csv", stringBuilder.ToString()); }
static Debugger() { for (int i = 24; i < 70; i++) { StringPool.PreAlloc(i, 2); } }
public virtual void PreProcess(IPrefabProcessor preProcess, GameObject rootObj, string name, bool serverside, bool clientside, bool bundling) { if (bundling) { return; } this.fullName = name; this.hierachyName = base.transform.GetRecursiveName(""); this.prefabID = StringPool.Get(name); this.instanceID = base.GetInstanceID(); this.worldPosition = base.transform.position; this.worldRotation = base.transform.rotation; this.worldForward = base.transform.forward; this.localPosition = base.transform.localPosition; this.localScale = base.transform.localScale; this.localRotation = base.transform.localRotation; if (serverside) { this.prefabAttribute = PrefabAttribute.server; this.gameManager = GameManager.server; this.isServer = true; } this.AttributeSetup(rootObj, name, serverside, clientside, bundling); if (serverside) { PrefabAttribute.server.Add(this.prefabID, this); } preProcess.RemoveComponent(this); preProcess.NominateForDeletion(base.gameObject); }
public override void ServerCommand(Item item, string command, BasePlayer player) { if (item.GetOwnerPlayer() != player) { bool flag = false; foreach (ItemContainer container in player.inventory.loot.containers) { if (item.GetRootContainer() == container) { flag = true; break; } } if (!flag) { return; } } if (!(command == "study") || !item.IsBlueprint()) { return; } ItemDefinition blueprintTargetDef = item.blueprintTargetDef; ItemBlueprint blueprint = blueprintTargetDef.Blueprint; bool flag2 = player.blueprints.IsUnlocked(blueprintTargetDef); if (flag2 && blueprint != null && blueprint.additionalUnlocks != null && blueprint.additionalUnlocks.Count > 0) { foreach (ItemDefinition additionalUnlock in blueprint.additionalUnlocks) { if (!player.blueprints.IsUnlocked(additionalUnlock)) { flag2 = false; } } } if (flag2 || Interface.CallHook("OnPlayerStudyBlueprint", player, item) != null) { return; } Item item2 = item; if (item.amount > 1) { item2 = item.SplitItem(1); } item2.UseItem(); player.blueprints.Unlock(blueprintTargetDef); if (blueprint != null && blueprint.additionalUnlocks != null && blueprint.additionalUnlocks.Count > 0) { foreach (ItemDefinition additionalUnlock2 in blueprint.additionalUnlocks) { player.blueprints.Unlock(additionalUnlock2); } } if (studyEffect.isValid) { Effect.server.Run(studyEffect.resourcePath, player, StringPool.Get("head"), Vector3.zero, Vector3.zero); } }
private GameObject Instantiate(string strPrefab, Vector3 pos, Quaternion rot) { if (!strPrefab.IsLower()) { Debug.LogWarning(string.Concat("Converting prefab name to lowercase: ", strPrefab)); strPrefab = strPrefab.ToLower(); } GameObject gameObject = this.FindPrefab(strPrefab); if (!gameObject) { Debug.LogError(string.Concat("Couldn't find prefab \"", strPrefab, "\"")); return(null); } GameObject gameObject1 = this.pool.Pop(StringPool.Get(strPrefab), pos, rot); if (gameObject1 != null) { gameObject1.transform.localScale = gameObject.transform.localScale; } else { gameObject1 = Instantiate.GameObject(gameObject, pos, rot); gameObject1.name = strPrefab; } if (!this.Clientside && this.Serverside && gameObject1.transform.parent == null) { SceneManager.MoveGameObjectToScene(gameObject1, Rust.Server.EntityScene); } return(gameObject1); }
public static string rpclog(Arg arg) { if (!rpclog_enabled) { return("RPC log is not enabled."); } List <Tuple <uint, ulong> > list = new List <Tuple <uint, ulong> >(); foreach (KeyValuePair <uint, TimeAverageValue> item in SingletonComponent <ServerMgr> .Instance.rpcHistory.dict) { list.Add(new Tuple <uint, ulong>(item.Key, item.Value.Calculate())); } TextTable textTable = new TextTable(); textTable.AddColumn("id"); textTable.AddColumn("name"); textTable.AddColumn("calls"); foreach (Tuple <uint, ulong> item2 in list.OrderByDescending((Tuple <uint, ulong> entry) => entry.Item2)) { if (item2.Item2 == 0L) { break; } string text = item2.Item1.ToString(); string text2 = StringPool.Get(item2.Item1); string text3 = item2.Item2.ToString(); textTable.AddRow(text, text2, text3); } return(textTable.ToString()); }
public static void print_prefabs(ConsoleSystem.Arg arg) { PrefabPoolCollection prefabPoolCollection = GameManager.server.pool; if (prefabPoolCollection.storage.Count == 0) { arg.ReplyWith("Prefab pool is empty."); return; } string str = arg.GetString(0, string.Empty); TextTable textTable = new TextTable(); textTable.AddColumn("id"); textTable.AddColumn("name"); textTable.AddColumn("count"); foreach (KeyValuePair <uint, PrefabPool> keyValuePair in prefabPoolCollection.storage) { string str1 = keyValuePair.Key.ToString(); string str2 = StringPool.Get(keyValuePair.Key); string str3 = keyValuePair.Value.Count.ToString(); if (!string.IsNullOrEmpty(str) && !str2.Contains(str, CompareOptions.IgnoreCase)) { continue; } textTable.AddRow(new string[] { str1, Path.GetFileNameWithoutExtension(str2), str3 }); } arg.ReplyWith(textTable.ToString()); }
void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo hitInfo) { try { if (entity is BasePlayer && hitInfo.Initiator is BasePlayer) { if (entity as BasePlayer == null || hitInfo == null) { return; } string bodypart = StringPool.Get(hitInfo.HitBone); if (bodypart == null || bodypart == "") { return; } string weapon = hitInfo.Weapon.GetItem().info.shortname; if (weapon == null || weapon == "") { return; } if (InList(weapon, bodypart)) { float modifier = configData.Weapons[weapon][bodypart]; hitInfo.damageTypes.ScaleAll(modifier); } } } catch (NullReferenceException ex) { } }
public void Awake() { FileSystem.iface = new FileSystem_AssetBundles(@"C:\Program Files (x86)\Steam\steamapps\common\RustStaging\Bundles\Bundles"); Transform prefabsParent = GameObject.FindGameObjectWithTag("Prefabs").transform; foreach (PrefabDataHolder pdh in GameObject.FindObjectsOfType <PrefabDataHolder>()) { if (!pdh.spawnOnPlay) { continue; } Debug.Log(StringPool.Get((pdh.prefabData.id))); GameObject g = FileSystem.Load <GameObject>(StringPool.Get((pdh.prefabData.id))); GameObject newObject = spawnPrefab(g, pdh.prefabData, prefabsParent); PrefabDataHolder prefabData = newObject.GetComponent <PrefabDataHolder>(); if (prefabData == null) { prefabData = newObject.AddComponent <PrefabDataHolder>(); } prefabData.prefabData = pdh.prefabData; Destroy(pdh.gameObject); } }
string formatBodyPartName(HitInfo hitInfo) { string bodypart = "Unknown"; bodypart = StringPool.Get(Convert.ToUInt32(hitInfo?.HitBone)) ?? "Unknown"; if ((bool)string.IsNullOrEmpty(bodypart)) { bodypart = "Unknown"; } for (int i = 0; i < 10; i++) { bodypart = bodypart.Replace(i.ToString(), ""); } bodypart = bodypart.Replace(".prefab", ""); bodypart = bodypart.Replace("L", ""); bodypart = bodypart.Replace("R", ""); bodypart = bodypart.Replace("_", ""); bodypart = bodypart.Replace(".", ""); bodypart = bodypart.Replace("right", ""); bodypart = bodypart.Replace("left", ""); bodypart = bodypart.Replace("tranform", ""); bodypart = bodypart.Replace("lowerjaweff", "jaw"); bodypart = bodypart.Replace("rarmpolevector", "arm"); bodypart = bodypart.Replace("connection", ""); bodypart = bodypart.Replace("uppertight", "tight"); bodypart = bodypart.Replace("fatjiggle", ""); bodypart = bodypart.Replace("fatend", ""); bodypart = bodypart.Replace("seff", ""); bodypart = bodypart.Replace("Unknown", "Bleed to death"); bodypart = bodypart.ToUpper(); return(bodypart); }
void Init() { LoadData(); autoTurretPrefabId = StringPool.Get(autoTurretPrefab); flameTurretPrefabId = StringPool.Get(flameTurretPrefab); permission.RegisterPermission("turretconfig.infiniteammo", this); adminOverride = GetConfig("Settings", "adminOverride", true); animalOverride = GetConfig("Settings", "animalOverride", false); sleepOverride = GetConfig("Settings", "sleepOverride", false); animals = GetConfig("Settings", "animals", GetPassiveAnimals()); infiniteAmmo = GetConfig("Settings", "infiniteAmmo", false); useGlobalDamageModifier = GetConfig("Settings", "useGlobalDamageModifier", false); globalDamageModifier = GetConfig("Settings", "globalDamageModifier", 1f); defaultBulletModifier = GetConfig("Auto", "defaultBulletModifier", 1f); defaultAutoHealth = GetConfig("Auto", "defaultAutoHealth", 1000f); defaultAimCone = GetConfig("Auto", "defaultAimCone", 5f); defaultSightRange = GetConfig("Auto", "defaultSightRange", 30f); defaultBulletSpeed = GetConfig("Auto", "defaultBulletSpeed", 10f); defaultAmmoType = GetConfig("Auto", "defaultAmmoType", "ammo.rifle"); bulletModifiers = GetConfig("Auto", "bulletModifiers", GetDefaultBulletModifiers()); bulletSpeeds = GetConfig("Auto", "bulletSpeeds", GetDefaultBulletSpeeds()); ammoTypes = GetConfig("Auto", "ammoTypes", GetDefaultAmmoTypes()); sightRanges = GetConfig("Auto", "sightRanges", GetDefaultSightRanges()); autoHealths = GetConfig("Auto", "autoHealths", GetDefaultAutoHealths()); aimCones = GetConfig("Auto", "aimCones", GetDefaultAimCones()); defaultArc = GetConfig("Flame", "defaultArc", 45f); defaultTriggerDuration = GetConfig("Flame", "defaultTriggerDuration", 5f); defaultFlameRange = GetConfig("Flame", "defaultFlameRange", 7f); defaultFlameRadius = GetConfig("Flame", "defaultFlameRadius", 4f); defaultFuelPerSec = GetConfig("Flame", "defaultFuelPerSec", 1f); defaultFlameHealth = GetConfig("Flame", "defaultFlameHealth", 300f); arcs = GetConfig("Flame", "arcs", GetDefaultArcs()); triggerDurations = GetConfig("Flame", "triggerDurations", GetDefaultTriggerDurations()); flameRanges = GetConfig("Flame", "flameRanges", GetDefaultFlameRanges()); flameRadiuses = GetConfig("Flame", "flameRadiuses", GetDefaultFlameRadiuses()); fuelPerSecs = GetConfig("Flame", "fuelPerSecs", GetDefaultFuelPerSecs()); flameHealths = GetConfig("Flame", "flameHealths", GetDefaultFlameHealths()); LoadPermissions(bulletModifiers); LoadPermissions(bulletSpeeds); LoadPermissions(ammoTypes); LoadPermissions(sightRanges); LoadPermissions(autoHealths); LoadPermissions(aimCones); LoadPermissions(arcs); LoadPermissions(triggerDurations); LoadPermissions(flameRanges); LoadPermissions(flameRadiuses); LoadPermissions(fuelPerSecs); LoadPermissions(flameHealths); }
/// <summary> /// Create a tree for the given document /// </summary> public XmlTree(XDocument doc) : base(ChunkTypes.RES_XML_TYPE) { strings = new StringPool(); resourceMap = new ResourceMap(); CreateNodes(doc.Root); }
/// <summary> /// Write out string pool with encrypt. /// </summary> /// <param name="stringPool">String pool.</param> /// <param name="writer">Binary data writer.</param> /// <returns>Size of bytes written out.</returns> public static uint Write(StringPool stringPool, DataWriter writer) { Helper.ThrowIfNull(stringPool); Helper.ThrowIfNull(writer); byte[] encryptedStringPool = new byte[stringPool.Length]; Microsoft.Tts.ServiceProvider.HTSVoiceDataEncrypt.EncryptStringPool( stringPool.ToArray(), encryptedStringPool); return writer.Write(encryptedStringPool); }
/// <summary> /// Creation ctor /// </summary> internal Package(Table table, int id, string name) : base(ChunkTypes.RES_TABLE_PACKAGE_TYPE) { this.table = table; this.id = id; this.name = name; keyStrings = new StringPool(); typeStrings = new StringPool(); }
/// <summary> /// Default ctor /// </summary> internal Table(ResReader reader) : base(reader, ChunkTypes.RES_TABLE_TYPE) { var packageCount = reader.ReadInt32(); strings = new StringPool(reader); for (var i = 0; i < packageCount; i++) { packages.Add(new Package(this, reader)); } }
public void LoadAndCreateTest() { var xml = new XmlTree(new MemoryStream(TestResource.AndroidManifest)); var pool = xml.StringPool; var stream = new MemoryStream(); var writer = new ResWriter(stream); pool.Write(writer); stream.Position = 0; var pool2 = new StringPool(new ResReader(stream)); Assert.AreEqual(pool.Count, pool2.Count); }
/// <summary> /// Default ctor /// </summary> internal Package(Table table, ResReader reader) : base(reader, ChunkTypes.RES_TABLE_PACKAGE_TYPE) { this.table = table; id = reader.ReadInt32(); name = reader.ReadFixedLenghtUnicodeString(128); var typeStringsOffset = reader.ReadInt32(); var lastPublicType = reader.ReadInt32(); var keyStringsOffset = reader.ReadInt32(); var lastPublicKey = reader.ReadInt32(); // Record offset var dataOffset = reader.Position; // Data typeStrings = new StringPool(reader); keyStrings = new StringPool(reader); TypeSpec currentTypeSpec = null; while (reader.Position - dataOffset < DataSize) { var chunkType = reader.PeekChunkType(); if (chunkType == ChunkTypes.RES_TABLE_TYPE_SPEC_TYPE) { currentTypeSpec = Read(reader, () => new TypeSpec(this, reader)); typeSpecs.Add(currentTypeSpec); } else if (chunkType == ChunkTypes.RES_TABLE_TYPE_TYPE) { if (currentTypeSpec == null) { throw new IOException("Invalid chunk sequence: content read before typeSpec."); } var parent = currentTypeSpec; var type = Read(reader, () => new Type(parent, reader)); currentTypeSpec.Add(type); } else { throw new IOException("Unexpected chunk type (" + chunkType + ")."); } } }
/// <summary> /// Reading ctor /// </summary> public XmlTree(ResReader reader) : base(reader, ChunkTypes.RES_XML_TYPE) { strings = new StringPool(reader); resourceMap = new ResourceMap(reader); while (true) { var tag = reader.PeekChunkType(); Node node; switch (tag) { case ChunkTypes.RES_XML_START_NAMESPACE_TYPE: node = new StartNamespace(reader, this); break; case ChunkTypes.RES_XML_START_ELEMENT_TYPE: node = new StartElement(reader, this); break; case ChunkTypes.RES_XML_CDATA_TYPE: node = new CData(reader, this); break; case ChunkTypes.RES_XML_END_ELEMENT_TYPE: node = new EndElement(reader, this); break; case ChunkTypes.RES_XML_END_NAMESPACE_TYPE: node = new EndNamespace(reader, this); break; default: throw new IOException(string.Format("Unexpected tag: 0x{0:X}", (int)tag)); } nodes.Add(node); if (tag == ChunkTypes.RES_XML_END_NAMESPACE_TYPE) break; } }
/// <summary> /// Compile Chinese tone data table into binary file. /// </summary> /// <param name="chineseToneDataFile">Path of Chinese tone data file.</param> /// <param name="outputStream">Output Stream.</param> /// <returns>ErrorSet.</returns> public static ErrorSet Compile(string chineseToneDataFile, Stream outputStream) { if (string.IsNullOrEmpty(chineseToneDataFile)) { throw new ArgumentNullException("chineseToneDataFile"); } if (outputStream == null) { throw new ArgumentNullException("outputStream"); } ErrorSet errorSet = new ErrorSet(); if (!File.Exists(chineseToneDataFile)) { errorSet.Add(ChineseToneCompilerError.DataFileNotFound, chineseToneDataFile); } else { BinaryWriter outputBinaryWriter = new BinaryWriter(outputStream, Encoding.Unicode); List<string> fileLines = new List<string>(Helper.FileLines(chineseToneDataFile, Encoding.Unicode)); List<string> wordListAABB = new List<string>(); List<string> wordListAAB = new List<string>(); int nTableIdx = -1; // Load words from raw data file to a word list for (int i = 0; i < fileLines.Count; ++i) { string fileLine = fileLines[i]; if (fileLine.Contains("[ABAB]")) { nTableIdx = 0; continue; } else if (fileLine.Contains("[AAB]")) { nTableIdx = 1; continue; } if ((nTableIdx == 0 || nTableIdx == 1) && !string.IsNullOrWhiteSpace(fileLines[i]) && !fileLines[i].StartsWith("//")) { // Remove comment at the end of line if (fileLine.Contains("//")) { fileLine = fileLine.Remove(fileLines[i].IndexOf("//")); } // Clean white spaces among the line fileLine = fileLine.Trim(); string[] segments = fileLine.Split( new string[] { PairSeparator }, StringSplitOptions.RemoveEmptyEntries); if (segments.Length == 2) { segments[0] = segments[0].Trim(); segments[1] = segments[1].Trim(); switch (nTableIdx) { case 0: wordListAABB.AddRange(segments); wordListAABB.Add(string.Empty); break; case 1: wordListAAB.AddRange(segments); wordListAAB.Add(string.Empty); break; default: break; } } else { errorSet.Add(ChineseToneCompilerError.InvalidPatternFormData); } } } if (errorSet.Count == 0) { // used for 4 bytes align. const int ALIGNCOUNT = 4; byte[] alignBytes = new byte[] { 0, 0, 0, 0 }; // Start writing binary to output stream // Write table count outputBinaryWriter.Write((uint)2); // Write word count in each table outputBinaryWriter.Write((uint)wordListAABB.Count); outputBinaryWriter.Write((uint)wordListAAB.Count); List<int> offsetList = new List<int>(); using (StringPool stringPool = new StringPool()) { // Put the words from word list to string pool StringPool.WordsToStringPool(wordListAABB, stringPool, offsetList); // Write the strings from string pool byte[] stringBuffer = stringPool.ToArray(); int nWordByteSize = sizeof(int) * offsetList.Count; int nBufferByteSize = stringBuffer.Length; int nUnAlignSize = stringBuffer.Length % ALIGNCOUNT; if (nUnAlignSize != 0) { nBufferByteSize += ALIGNCOUNT - nUnAlignSize; } int nTableByteSize = nWordByteSize + nBufferByteSize; // Write offset of each table outputBinaryWriter.Write((uint)nTableByteSize); // Write offset of each word offsetList.ForEach(x => outputBinaryWriter.Write((uint)x)); outputBinaryWriter.Write(stringBuffer, 0, stringBuffer.Length); // Fill 0 to align the memory by 4 bytes if (stringBuffer.Length % ALIGNCOUNT != 0) { outputBinaryWriter.Write(alignBytes, 0, ALIGNCOUNT - (stringBuffer.Length % ALIGNCOUNT)); } } offsetList = new List<int>(); using (StringPool stringPool = new StringPool()) { // Put the words from word list to string pool StringPool.WordsToStringPool(wordListAAB, stringPool, offsetList); // Write the strings from string pool byte[] stringBuffer = stringPool.ToArray(); int nWordByteSize = sizeof(int) * offsetList.Count; int nBufferByteSize = stringBuffer.Length; int nUnAlignSize = stringBuffer.Length % ALIGNCOUNT; if (nUnAlignSize != 0) { nBufferByteSize += ALIGNCOUNT - nUnAlignSize; } int nTableByteSize = nWordByteSize + nBufferByteSize; // Write offset of each table outputBinaryWriter.Write((uint)nTableByteSize); // Write offset of each word offsetList.ForEach(x => outputBinaryWriter.Write((uint)x)); outputBinaryWriter.Write(stringBuffer, 0, stringBuffer.Length); // Fill 0 to align the memory by 4 bytes if (stringBuffer.Length % ALIGNCOUNT != 0) { outputBinaryWriter.Write(alignBytes, 0, ALIGNCOUNT - (stringBuffer.Length % ALIGNCOUNT)); } } } } return errorSet; }
/// <summary> /// Creation ctor /// </summary> public Table(string packageName) : base(ChunkTypes.RES_TABLE_TYPE) { strings = new StringPool(); packages.Add(new Package(this, 0x7f, packageName)); }
/// <summary> /// Save pre-selection forest. /// </summary> /// <param name="decisionForest">The forest with each tree corresponding to a unit.</param> /// <param name="candidateGroups">The candidate group collection.</param> /// <param name="unitCandidateNameIds">Given candidate idx.</param> /// <param name="customFeatures">Cusotmized linguistic feature list.</param> /// <param name="outputPath">The output path.</param> public void Write(DecisionForest decisionForest, ICollection<CandidateGroup> candidateGroups, IDictionary<string, int> unitCandidateNameIds, HashSet<string> customFeatures, string outputPath) { foreach (Question question in decisionForest.QuestionList) { question.Language = _phoneSet.Language; question.ValueSetToCodeValueSet(_posSet, _phoneSet, customFeatures); } FileStream file = new FileStream(outputPath, FileMode.Create); try { using (DataWriter writer = new DataWriter(file)) { file = null; uint position = 0; // Write header section place holder PreselectionFileHeader header = new PreselectionFileHeader(); position += (uint)header.Write(writer); HtsFontSerializer serializer = new HtsFontSerializer(); // Write feature, question and prepare string pool HtsQuestionSet questionSet = new HtsQuestionSet { Items = decisionForest.QuestionList, Header = new HtsQuestionSetHeader { HasQuestionName = false }, CustomFeatures = customFeatures, }; using (StringPool stringPool = new StringPool()) { Dictionary<string, uint> questionIndexes = new Dictionary<string, uint>(); header.QuestionOffset = position; header.QuestionSize = serializer.Write( questionSet, writer, stringPool, questionIndexes, customFeatures); position += header.QuestionSize; // Write leaf referenced data to buffer IEnumerable<INodeData> dataNodes = GetCandidateNodes(candidateGroups); using (MemoryStream candidateSetBuffer = new MemoryStream()) { Dictionary<string, int> namedSetOffset = new Dictionary<string, int>(); int candidateSetSize = HtsFontSerializer.Write( dataNodes, new DataWriter(candidateSetBuffer), namedSetOffset); // Write decision forest Dictionary<string, uint[]> namedOffsets = namedSetOffset.ToDictionary(p => p.Key, p => new[] { (uint)p.Value }); header.DecisionTreeSectionOffset = position; header.DecisionTreeSectionSize = (uint)Write(decisionForest, unitCandidateNameIds, questionIndexes, questionSet, namedOffsets, new DecisionForestSerializer(), writer); position += header.DecisionTreeSectionSize; // Write string pool header.StringPoolOffset = position; header.StringPoolSize = HtsFontSerializer.Write(stringPool, writer); position += header.StringPoolSize; // Write leaf referenced data header.CandidateSetSectionOffset = position; header.CandidateSetSectionSize = writer.Write(candidateSetBuffer.ToArray()); position += header.CandidateSetSectionSize; } // Write header section place holder using (PositionRecover recover = new PositionRecover(writer, 0)) { header.Write(writer); } } } } finally { if (null != file) { file.Dispose(); } } }
/// <summary> /// Save feature set. /// </summary> /// <param name="writer">Binary data writer.</param> /// <param name="stringPool">String pool.</param> /// <param name="language">Language.</param> /// <param name="schemaFile">Schema file.</param> /// <param name="outVarFile">Out var file.</param> /// <param name="phoneToIdIndexes">Phone id mapping.</param> /// <returns>Size of bytes written out.</returns> public uint Write(DataWriter writer, StringPool stringPool, Language language, string schemaFile, string outVarFile, Dictionary<string, string> phoneToIdIndexes) { Helper.ThrowIfNull(writer); Helper.ThrowIfNull(language); Helper.ThrowIfNull(phoneToIdIndexes); Helper.ThrowIfFileNotExist(schemaFile); Helper.ThrowIfFileNotExist(outVarFile); uint size = 0; size += WriteSchema(language, schemaFile, phoneToIdIndexes, writer, stringPool); size += WriteOutVariance(outVarFile, writer); Debug.Assert(size % sizeof(uint) == 0, "Data must be 4-byte aligned."); return size; }
/// <summary> /// Save schema (with feature set and it's value group, mean, variance). /// </summary> /// <param name="language">The language.</param> /// <param name="schemaFile">The schema File.</param> /// <param name="phoneToIdIndexes">Phone To Id Indexes.</param> /// <param name="writer">Writer.</param> /// <param name="stringPool">String pool.</param> /// <returns>Size of bytes written out.</returns> public uint WriteSchema(Language language, string schemaFile, Dictionary<string, string> phoneToIdIndexes, DataWriter writer, StringPool stringPool) { Helper.ThrowIfFileNotExist(schemaFile); Helper.ThrowIfNull(phoneToIdIndexes); Helper.ThrowIfNull(writer); Helper.ThrowIfNull(stringPool); Helper.ThrowIfNull(language); uint size = 0; LexicalAttributeSchema schema = new LexicalAttributeSchema(language); schema.Load(schemaFile); List<string> stateFeatureList = new List<string>(); List<string> featureList = new List<string>(); int stateFeatureCount = 0; for (int i = 0; i < schema.Categories.Count; i++) { string name = schema.Categories[i].Name.ToLower(); if (name.IndexOf("state") >= 0) { stateFeatureCount++; if (!stateFeatureList.Contains(name)) { stateFeatureList.Add(name); } } if (!featureList.Contains(name)) { featureList.Add(name); } } // write state feature count. size += writer.Write((uint)stateFeatureList.Count); size += writer.Write((uint)stateFeatureCount); // write total feature count. size += writer.Write((uint)featureList.Count()); Dictionary<string, uint> featureIndex = new Dictionary<string, uint>(); uint index = 0; foreach (string feature in featureList) { size += writer.Write((uint)stringPool.Length); stringPool.PutString(feature); featureIndex.Add(feature, index++); } // write feature category size += writer.Write((uint)schema.Categories.Count); for (int i = 0; i < schema.Categories.Count; i++) { string featureName = schema.Categories[i].Name.ToLower(); // feature index size += writer.Write((uint)featureIndex[featureName]); // mean size += writer.Write(schema.Categories[i].Mean); // invStdDev size += writer.Write(schema.Categories[i].InvStdDev); // value count size += writer.Write((uint)schema.Categories[i].Values.Count); for (int k = 0; k < schema.Categories[i].Values.Count; k++) { string valueName = schema.Categories[i].Values[k].Name.ToLower(); string id = string.Empty; if (phoneToIdIndexes.ContainsKey(valueName) && featureName.IndexOf("phoneidentity") >= 0) { id = phoneToIdIndexes[valueName]; } else { id = valueName; } try { size += writer.Write(uint.Parse(id)); } catch (System.FormatException) { continue; } } } Debug.Assert(size % sizeof(uint) == 0, "Data must be 4-byte aligned."); return size; }
public static ErrorSet Compile(string rnnModelPath, Stream outputStream, Collection<string> addedFileNames) { if (string.IsNullOrEmpty(rnnModelPath)) { throw new ArgumentNullException("rnnModelPath"); } if (outputStream == null) { throw new ArgumentNullException("outputStream"); } if (addedFileNames == null) { throw new ArgumentNullException("addedFileNames"); } ErrorSet errorSet = new ErrorSet(); if (!File.Exists(rnnModelPath)) { errorSet.Add(RNNModelCompilerError.ModelDataNotFound, rnnModelPath); } else { BinaryWriter bw = new BinaryWriter(outputStream, Encoding.Unicode); Dictionary<string, float> polyCharactersInfo = null; List<string> polyphones = null; List<float> thresholds = null; // load polyphonic characters that should be enabled in product. string polyphonicCharFile = Path.Combine(new DirectoryInfo(Path.GetDirectoryName(rnnModelPath)).Parent.FullName, "RNNPolyphoneList.txt"); if (File.Exists(polyphonicCharFile)) { // If the list file is existed, load it. polyCharactersInfo = LoadPolyphonicInfo(polyphonicCharFile, errorSet); } else { errorSet.Add(RNNModelCompilerError.PolyphonicCharFileNotFound, polyphonicCharFile); } polyphones = GetPolyphonicChars(polyCharactersInfo); thresholds = GetPolyphonicThreshold(polyCharactersInfo); uint polyCharCount = 0; uint modelOffset = 0; // write the count of polyphonic characters and polyphonic characters using (StringPool plycharSp = new StringPool()) { Collection<int> polycharOffsets = new Collection<int>(); StringPool.WordsToStringPool(polyphones, plycharSp, polycharOffsets); polyCharCount = (uint)polycharOffsets.Count; bw.Write(modelOffset); bw.Write(polyCharCount); foreach (float threshold in thresholds) { bw.Write(threshold); } byte[] plycharPool = plycharSp.ToArray(); foreach (int offset in polycharOffsets) { bw.Write((uint)offset); } bw.Write(plycharPool, 0, plycharPool.Length); } modelOffset = (uint)bw.BaseStream.Position; // write rnn models using (FileStream fs = new FileStream(rnnModelPath, FileMode.Open, FileAccess.Read)) using (BinaryReader br = new BinaryReader(fs)) { bw.Write(br.ReadBytes((int)fs.Length)); } bw.Flush(); bw.Seek(0, SeekOrigin.Begin); bw.Write(modelOffset); bw.Flush(); addedFileNames.Add(rnnModelPath); } return errorSet; }
/// <summary> /// Collect the words in the file and save them into string pool together with /// The offset list. /// </summary> /// <param name="filePath">File path containing words.</param> /// <param name="stringPool">String pool.</param> /// <param name="offsets">Offset list.</param> /// <param name="sort">Whether the word in the string pool are sorted.</param> /// <param name="errorSet">Error set.</param> /// <returns>Number of words.</returns> public static int LoadWordsIntoStringPool(string filePath, StringPool stringPool, ICollection<int> offsets, bool sort, ErrorSet errorSet) { if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException("filePath"); } if (stringPool == null) { throw new ArgumentNullException("stringPool"); } if (offsets == null) { throw new ArgumentNullException("offsets"); } if (errorSet == null) { throw new ArgumentNullException("errorSet"); } List<string> words = new List<string>(); errorSet.Merge(LoadWordsIntoWordList(filePath, words, sort)); StringPool.WordsToStringPool(words, stringPool, offsets); return words.Count; }
public static ErrorSet Compile(string crfModelDir, Stream outputStream, Collection<string> addedFileNames, Language lang) { if (string.IsNullOrEmpty(crfModelDir)) { throw new ArgumentNullException("crfModelDir"); } if (outputStream == null) { throw new ArgumentNullException("outputStream"); } if (addedFileNames == null) { throw new ArgumentNullException("addedFileNames"); } ErrorSet errorSet = new ErrorSet(); if (!Directory.Exists(crfModelDir)) { errorSet.Add(CrfModelCompilerError.DataFolderNotFound, crfModelDir); } else { BinaryWriter bw = new BinaryWriter(outputStream, Encoding.Unicode); List<byte[]> crfModels = new List<byte[]>(); List<string> crfTags = new List<string>(); Dictionary<string, string> localizedNameMapping = null; // if language = zh-cn or ja-jp, we should use their localized name as crf model tag. if (lang == Language.ZhCN || lang == Language.JaJP) { string crfModelNameMappingFile = Path.Combine(new DirectoryInfo(crfModelDir).Parent.FullName, "CRFLocalizedMapping.txt"); if (File.Exists(crfModelNameMappingFile)) { // If the mapping file is existed, load it. localizedNameMapping = LocalizeCRFModelName(crfModelNameMappingFile, errorSet); } else { errorSet.Add(CrfModelCompilerError.MappingFileNotFound, crfModelNameMappingFile); } } string[] crfModelFileNames = Directory.GetFiles(crfModelDir, "*.crf", SearchOption.TopDirectoryOnly); foreach (string crfModelFileName in crfModelFileNames) { if (localizedNameMapping != null) { // If mapping is existed, replace the crfTag name. string crfModelName = Path.GetFileName(crfModelFileName); if (localizedNameMapping.ContainsKey(crfModelName)) { crfTags.Add(localizedNameMapping[crfModelName].ToUpper()); // case insensitive } } else { crfTags.Add(Path.GetFileNameWithoutExtension(crfModelFileName).ToUpper()); // case insensitive } using (FileStream fs = new FileStream(crfModelFileName, FileMode.Open, FileAccess.Read)) using (BinaryReader br = new BinaryReader(fs)) { crfModels.Add(br.ReadBytes((int)fs.Length)); } addedFileNames.Add(crfModelFileName); } using (StringPool crfModelSp = new StringPool()) { using (StringPool crfTagSp = new StringPool()) { Collection<int> crfModelsOffsets = new Collection<int>(); Collection<int> crfTagsOffsets = new Collection<int>(); // Add models to StringPool foreach (byte[] model in crfModels) { crfModelsOffsets.Add(crfModelSp.PutBuffer(model)); } // Add tags to StringPool StringPool.WordsToStringPool(crfTags, crfTagSp, crfTagsOffsets); uint tagOffset = 0; uint modelOffset = 0; bw.Write(tagOffset); bw.Write(modelOffset); bw.Write((uint)crfTagsOffsets.Count); for (int i = 0; i < crfModelsOffsets.Count; i++) { uint offset = (uint)crfModelsOffsets[i]; if ((offset % 4) != 0) { errorSet.Add(CrfModelCompilerError.InvalidCrfModel, crfModelFileNames[i]); } bw.Write(offset); } foreach (int offset in crfTagsOffsets) { bw.Write((uint)offset); } modelOffset = (uint)bw.BaseStream.Position; Debug.Assert((modelOffset % 4) == 0); byte[] crfModelPool = crfModelSp.ToArray(); bw.Write(crfModelPool, 0, crfModelPool.Length); tagOffset = (uint)bw.BaseStream.Position; Debug.Assert((tagOffset % 4) == 0); byte[] crfTagPool = crfTagSp.ToArray(); bw.Write(crfTagPool, 0, crfTagPool.Length); bw.Flush(); // Update offset value. bw.Seek(0, SeekOrigin.Begin); bw.Write(tagOffset); bw.Write(modelOffset); bw.Flush(); } } } return errorSet; }
private void Dispose(bool disposing) { if (disposing) { if (_stringPool != null) { _stringPool.Dispose(); _disposed = true; _stringPool = null; } } }
/// <summary> /// Write the pst data. /// </summary> /// <param name="pstFile">The pst file name to be stored.</param> /// <param name="data">The pst data to be write.</param> /// <param name="ttsPhoneSet">The tts Phone set.</param> /// <param name="ttsPosSet">The tts pst set.</param> public void WritePSTData(string pstFile, PSTData data, TtsPhoneSet ttsPhoneSet, TtsPosSet ttsPosSet) { foreach (Question question in data.DecisionForest.QuestionList) { question.Language = ttsPhoneSet.Language; question.ValueSetToCodeValueSet(ttsPosSet, ttsPhoneSet, data.CustomFeatures); } FileStream file = new FileStream(pstFile, FileMode.Create); try { using (DataWriter writer = new DataWriter(file)) { file = null; uint position = 0; // Write header section place holder PreselectionFileHeader header = new PreselectionFileHeader(); position += (uint)header.Write(writer); HtsFontSerializer serializer = new HtsFontSerializer(); using (StringPool stringPool = new StringPool()) { Dictionary<string, uint> questionIndexes = new Dictionary<string, uint>(); header.QuestionOffset = position; header.QuestionSize = serializer.Write( data.QuestionSet, writer, stringPool, questionIndexes, data.CustomFeatures); position += header.QuestionSize; // Write leaf referenced data to buffer List<CandidateSetData> dataNodes = data.CadidateSets; int val = data.CadidateSets.Sum(c => c.Candidates.Count); using (MemoryStream candidateSetBuffer = new MemoryStream()) { Dictionary<string, int> namedSetOffset = new Dictionary<string, int>(); int candidateSetSize = HtsFontSerializer.Write( dataNodes, new DataWriter(candidateSetBuffer), namedSetOffset); // Write decision forest Dictionary<string, uint[]> namedOffsets = namedSetOffset.ToDictionary(p => p.Key, p => new[] { (uint)p.Value }); header.DecisionTreeSectionOffset = position; header.DecisionTreeSectionSize = (uint)Write(data.DecisionForest, data.TreeIndexes, questionIndexes, data.QuestionSet, namedOffsets, new DecisionForestSerializer(), writer); position += header.DecisionTreeSectionSize; // Write string pool header.StringPoolOffset = position; header.StringPoolSize = HtsFontSerializer.Write(stringPool, writer); position += header.StringPoolSize; // Write leaf referenced data header.CandidateSetSectionOffset = position; header.CandidateSetSectionSize = writer.Write(candidateSetBuffer.ToArray()); position += header.CandidateSetSectionSize; } // Write header section place holder using (PositionRecover recover = new PositionRecover(writer, 0)) { header.Write(writer); } } } } finally { if (null != file) { file.Dispose(); } } }
/// <summary> /// Reading ctor /// </summary> internal static string Read(ResReader reader, StringPool pool) { var index = reader.ReadInt32(); return (index < 0) ? null : pool[index]; }
/// <summary> /// Write helper /// </summary> internal static void Write(ResWriter writer, StringPool pool, string value, int resourceId = -1) { var index = (value == null) ? -1 : pool.Get(value, resourceId); writer.WriteInt32(index); }