Index: ps/trunk/binaries/data/mods/public/globalscripts/Resources.js
===================================================================
--- ps/trunk/binaries/data/mods/public/globalscripts/Resources.js (revision 20585)
+++ ps/trunk/binaries/data/mods/public/globalscripts/Resources.js (revision 20586)
@@ -1,103 +1,86 @@
/**
* Since the AI context can't access JSON functions, it gets passed an object
* containing the information from `GuiInterface.js::GetSimulationState()`.
*/
function Resources()
{
- let jsonFiles = [];
- // Simulation context
- if (Engine.FindJSONFiles)
- {
- jsonFiles = Engine.FindJSONFiles("resources", false);
- for (let file in jsonFiles)
- jsonFiles[file] = "simulation/data/resources/" + jsonFiles[file] + ".json";
- }
- // GUI context
- else if (Engine.BuildDirEntList)
- jsonFiles = Engine.BuildDirEntList("simulation/data/resources/", "*.json", false);
- else
- {
- error("Resources: JSON functions are not available");
- return;
- }
-
this.resourceData = [];
this.resourceDataObj = {};
this.resourceCodes = [];
this.resourceNames = {};
- for (let filename of jsonFiles)
+ for (let filename of Engine.ListDirectoryFiles("simulation/data/resources/", "*.json", false))
{
let data = Engine.ReadJSONFile(filename);
if (!data)
continue;
if (data.code != data.code.toLowerCase())
warn("Resource codes should use lower case: " + data.code);
// Treasures are supported for every specified resource
if (data.code == "treasure")
{
error("Encountered resource with reserved keyword: " + data.code);
continue;
}
this.resourceData.push(data);
this.resourceDataObj[data.code] = data;
this.resourceCodes.push(data.code);
this.resourceNames[data.code] = data.name;
for (let subres in data.subtypes)
this.resourceNames[subres] = data.subtypes[subres];
}
// Sort arrays by specified order
let resSort = (a, b) =>
a.order < b.order ? -1 :
a.order > b.order ? +1 : 0;
this.resourceData.sort(resSort);
this.resourceCodes.sort((a, b) => resSort(
this.resourceData.find(resource => resource.code == a),
this.resourceData.find(resource => resource.code == b)
));
deepfreeze(this.resourceData);
deepfreeze(this.resourceDataObj);
deepfreeze(this.resourceCodes);
deepfreeze(this.resourceNames);
}
/**
* Returns the objects defined in the JSON files for all availbale resources,
* ordered as defined in these files.
*/
Resources.prototype.GetResources = function()
{
return this.resourceData;
};
/**
* Returns the object defined in the JSON file for the given resource.
*/
Resources.prototype.GetResource = function(type)
{
return this.resourceDataObj[type];
};
/**
* Returns an array containing all resource codes ordered as defined in the resource files.
* For example ["food", "wood", "stone", "metal"].
*/
Resources.prototype.GetCodes = function()
{
return this.resourceCodes;
};
/**
* Returns an object mapping resource codes to translatable resource names. Includes subtypes.
* For example { "food": "Food", "fish": "Fish", "fruit": "Fruit", "metal": "Metal", ... }
*/
Resources.prototype.GetNames = function()
{
return this.resourceNames;
};
Index: ps/trunk/binaries/data/mods/public/globalscripts/Templates.js
===================================================================
--- ps/trunk/binaries/data/mods/public/globalscripts/Templates.js (revision 20585)
+++ ps/trunk/binaries/data/mods/public/globalscripts/Templates.js (revision 20586)
@@ -1,518 +1,517 @@
/**
* Loads history and gameplay data of all civs.
- * Can be used from GUI and rmgen (because the simulation functions differ currently).
*
* @param selectableOnly {boolean} - Only load civs that can be selected
* in the gamesetup. Scenario maps might set non-selectable civs.
*/
function loadCivFiles(selectableOnly)
{
let propertyNames = [
"Code", "Culture", "Name", "Emblem", "History", "Music", "Factions", "CivBonuses", "TeamBonuses",
"Structures", "StartEntities", "Formations", "AINames", "SkirmishReplacements", "SelectableInGameSetup"];
let civData = {};
- for (let filename of Engine.BuildDirEntList("simulation/data/civs/", "*.json", false))
+ for (let filename of Engine.ListDirectoryFiles("simulation/data/civs/", "*.json", false))
{
let data = Engine.ReadJSONFile(filename);
for (let prop of propertyNames)
if (data[prop] === undefined)
throw new Error(filename + " doesn't contain " + prop);
if (!selectableOnly || data.SelectableInGameSetup)
civData[data.Code] = data;
}
return civData;
}
/**
* Gets an array of all classes for this identity template
*/
function GetIdentityClasses(template)
{
var classList = [];
if (template.Classes && template.Classes._string)
classList = classList.concat(template.Classes._string.split(/\s+/));
if (template.VisibleClasses && template.VisibleClasses._string)
classList = classList.concat(template.VisibleClasses._string.split(/\s+/));
if (template.Rank)
classList = classList.concat(template.Rank);
return classList;
}
/**
* Gets an array with all classes for this identity template
* that should be shown in the GUI
*/
function GetVisibleIdentityClasses(template)
{
if (template.VisibleClasses && template.VisibleClasses._string)
return template.VisibleClasses._string.split(/\s+/);
return [];
}
/**
* Check if a given list of classes matches another list of classes.
* Useful f.e. for checking identity classes.
*
* @param classes - List of the classes to check against.
* @param match - Either a string in the form
* "Class1 Class2+Class3"
* where spaces are handled as OR and '+'-signs as AND,
* and ! is handled as NOT, thus Class1+!Class2 = Class1 AND NOT Class2.
* Or a list in the form
* [["Class1"], ["Class2", "Class3"]]
* where the outer list is combined as OR, and the inner lists are AND-ed.
* Or a hybrid format containing a list of strings, where the list is
* combined as OR, and the strings are split by space and '+' and AND-ed.
*
* @return undefined if there are no classes or no match object
* true if the the logical combination in the match object matches the classes
* false otherwise.
*/
function MatchesClassList(classes, match)
{
if (!match || !classes)
return undefined;
// Transform the string to an array
if (typeof match == "string")
match = match.split(/\s+/);
for (let sublist of match)
{
// If the elements are still strings, split them by space or by '+'
if (typeof sublist == "string")
sublist = sublist.split(/[+\s]+/);
if (sublist.every(c => (c[0] == "!" && classes.indexOf(c.substr(1)) == -1)
|| (c[0] != "!" && classes.indexOf(c) != -1)))
return true;
}
return false;
}
/**
* Gets the value originating at the value_path as-is, with no modifiers applied.
*
* @param {object} template - A valid template as returned from a template loader.
* @param {string} value_path - Route to value within the xml template structure.
* @return {number}
*/
function GetBaseTemplateDataValue(template, value_path)
{
let current_value = template;
for (let property of value_path.split("/"))
current_value = current_value[property] || 0;
return +current_value;
}
/**
* Gets the value originating at the value_path with the modifiers dictated by the mod_key applied.
*
* @param {object} template - A valid template as returned from a template loader.
* @param {string} value_path - Route to value within the xml template structure.
* @param {string} mod_key - Tech modification key, if different from value_path.
* @param {number} player - Optional player id.
* @param {object} modifiers - Value modifiers from auto-researched techs, unit upgrades,
* etc. Optional as only used if no player id provided.
* @return {number} Modifier altered value.
*/
function GetModifiedTemplateDataValue(template, value_path, mod_key, player, modifiers={})
{
let current_value = GetBaseTemplateDataValue(template, value_path);
mod_key = mod_key || value_path;
if (player)
current_value = ApplyValueModificationsToTemplate(mod_key, current_value, player, template);
else if (modifiers)
current_value = GetTechModifiedProperty(modifiers, GetIdentityClasses(template.Identity), mod_key, current_value);
// Using .toFixed() to get around spidermonkey's treatment of numbers (3 * 1.1 = 3.3000000000000003 for instance).
return +current_value.toFixed(8);
}
/**
* Get information about a template with or without technology modifications.
*
* NOTICE: The data returned here should have the same structure as
* the object returned by GetEntityState and GetExtendedEntityState!
*
* @param {object} template - A valid template as returned by the template loader.
* @param {number} player - An optional player id to get the technology modifications
* of properties.
* @param {object} auraTemplates - In the form of { key: { "auraName": "", "auraDescription": "" } }.
* @param {object} resources - An instance of the Resources prototype.
* @param {object} damageTypes - An instance of the DamageTypes prototype.
* @param {object} modifiers - Modifications from auto-researched techs, unit upgrades
* etc. Optional as only used if there's no player
* id provided.
*/
function GetTemplateDataHelper(template, player, auraTemplates, resources, damageTypes, modifiers={})
{
// Return data either from template (in tech tree) or sim state (ingame).
// @param {string} value_path - Route to the value within the template.
// @param {string} mod_key - Modification key, if not the same as the value_path.
let getEntityValue = function(value_path, mod_key) {
return GetModifiedTemplateDataValue(template, value_path, mod_key, player, modifiers);
};
let ret = {};
if (template.Armour)
{
ret.armour = {};
for (let damageType of damageTypes.GetTypes())
ret.armour[damageType] = getEntityValue("Armour/" + damageType);
}
if (template.Attack)
{
ret.attack = {};
for (let type in template.Attack)
{
let getAttackStat = function(stat) {
return getEntityValue("Attack/" + type + "/" + stat);
};
if (type == "Capture")
ret.attack.Capture = {
"value": getAttackStat("Value")
};
else
{
ret.attack[type] = {
"minRange": getAttackStat("MinRange"),
"maxRange": getAttackStat("MaxRange"),
"elevationBonus": getAttackStat("ElevationBonus")
};
for (let damageType of damageTypes.GetTypes())
ret.attack[type][damageType] = getAttackStat(damageType);
ret.attack[type].elevationAdaptedRange = Math.sqrt(ret.attack[type].maxRange *
(2 * ret.attack[type].elevationBonus + ret.attack[type].maxRange));
}
ret.attack[type].repeatTime = getAttackStat("RepeatTime");
if (template.Attack[type].Splash)
{
ret.attack[type].splash = {
// true if undefined
"friendlyFire": template.Attack[type].Splash.FriendlyFire != "false",
"shape": template.Attack[type].Splash.Shape
};
for (let damageType of damageTypes.GetTypes())
ret.attack[type].splash[damageType] = getAttackStat("Splash/" + damageType);
}
}
}
if (template.DeathDamage)
{
ret.deathDamage = {
"friendlyFire": template.DeathDamage.FriendlyFire != "false"
};
for (let damageType of damageTypes.GetTypes())
ret.deathDamage[damageType] = getEntityValue("DeathDamage/" + damageType);
}
if (template.Auras)
{
ret.auras = {};
for (let auraID of template.Auras._string.split(/\s+/))
{
let aura = auraTemplates[auraID];
ret.auras[auraID] = {
"name": aura.auraName,
"description": aura.auraDescription || null,
"radius": aura.radius || null
};
}
}
if (template.BuildingAI)
ret.buildingAI = {
"defaultArrowCount": Math.round(getEntityValue("BuildingAI/DefaultArrowCount")),
"garrisonArrowMultiplier": getEntityValue("BuildingAI/GarrisonArrowMultiplier"),
"maxArrowCount": Math.round(getEntityValue("BuildingAI/MaxArrowCount"))
};
if (template.BuildRestrictions)
{
// required properties
ret.buildRestrictions = {
"placementType": template.BuildRestrictions.PlacementType,
"territory": template.BuildRestrictions.Territory,
"category": template.BuildRestrictions.Category,
};
// optional properties
if (template.BuildRestrictions.Distance)
{
ret.buildRestrictions.distance = {
"fromClass": template.BuildRestrictions.Distance.FromClass,
};
if (template.BuildRestrictions.Distance.MinDistance)
ret.buildRestrictions.distance.min = getEntityValue("BuildRestrctions/Distance/MinDistance");
if (template.BuildRestrictions.Distance.MaxDistance)
ret.buildRestrictions.distance.max = getEntityValue("BuildRestrctions/Distance/MaxDistance");
}
}
if (template.TrainingRestrictions)
ret.trainingRestrictions = {
"category": template.TrainingRestrictions.Category,
};
if (template.Cost)
{
ret.cost = {};
for (let resCode in template.Cost.Resources)
ret.cost[resCode] = getEntityValue("Cost/Resources/" + resCode);
if (template.Cost.Population)
ret.cost.population = getEntityValue("Cost/Population");
if (template.Cost.PopulationBonus)
ret.cost.populationBonus = getEntityValue("Cost/PopulationBonus");
if (template.Cost.BuildTime)
ret.cost.time = getEntityValue("Cost/BuildTime");
}
if (template.Footprint)
{
ret.footprint = { "height": template.Footprint.Height };
if (template.Footprint.Square)
ret.footprint.square = {
"width": +template.Footprint.Square["@width"],
"depth": +template.Footprint.Square["@depth"]
};
else if (template.Footprint.Circle)
ret.footprint.circle = { "radius": +template.Footprint.Circle["@radius"] };
else
warn("GetTemplateDataHelper(): Unrecognized Footprint type");
}
if (template.GarrisonHolder)
{
ret.garrisonHolder = {
"buffHeal": getEntityValue("GarrisonHolder/BuffHeal")
};
if (template.GarrisonHolder.Max)
ret.garrisonHolder.capacity = getEntityValue("GarrisonHolder/Max");
}
if (template.Heal)
ret.heal = {
"hp": getEntityValue("Heal/HP"),
"range": getEntityValue("Heal/Range"),
"rate": getEntityValue("Heal/Rate")
};
if (template.ResourceGatherer)
{
ret.resourceGatherRates = {};
let baseSpeed = getEntityValue("ResourceGatherer/BaseSpeed");
for (let type in template.ResourceGatherer.Rates)
ret.resourceGatherRates[type] = getEntityValue("ResourceGatherer/Rates/"+ type) * baseSpeed;
}
if (template.ResourceTrickle)
{
ret.resourceTrickle = {
"interval": +template.ResourceTrickle.Interval,
"rates": {}
};
for (let type in template.ResourceTrickle.Rates)
ret.resourceTrickle.rates[type] = getEntityValue("ResourceTrickle/Rates/" + type);
}
if (template.Loot)
{
ret.loot = {};
for (let type in template.Loot)
ret.loot[type] = getEntityValue("Loot/"+ type);
}
if (template.Obstruction)
{
ret.obstruction = {
"active": ("" + template.Obstruction.Active == "true"),
"blockMovement": ("" + template.Obstruction.BlockMovement == "true"),
"blockPathfinding": ("" + template.Obstruction.BlockPathfinding == "true"),
"blockFoundation": ("" + template.Obstruction.BlockFoundation == "true"),
"blockConstruction": ("" + template.Obstruction.BlockConstruction == "true"),
"disableBlockMovement": ("" + template.Obstruction.DisableBlockMovement == "true"),
"disableBlockPathfinding": ("" + template.Obstruction.DisableBlockPathfinding == "true"),
"shape": {}
};
if (template.Obstruction.Static)
{
ret.obstruction.shape.type = "static";
ret.obstruction.shape.width = +template.Obstruction.Static["@width"];
ret.obstruction.shape.depth = +template.Obstruction.Static["@depth"];
}
else if (template.Obstruction.Unit)
{
ret.obstruction.shape.type = "unit";
ret.obstruction.shape.radius = +template.Obstruction.Unit["@radius"];
}
else
ret.obstruction.shape.type = "cluster";
}
if (template.Pack)
ret.pack = {
"state": template.Pack.State,
"time": getEntityValue("Pack/Time"),
};
if (template.Health)
ret.health = Math.round(getEntityValue("Health/Max"));
if (template.Identity)
{
ret.selectionGroupName = template.Identity.SelectionGroupName;
ret.name = {
"specific": (template.Identity.SpecificName || template.Identity.GenericName),
"generic": template.Identity.GenericName
};
ret.icon = template.Identity.Icon;
ret.tooltip = template.Identity.Tooltip;
ret.requiredTechnology = template.Identity.RequiredTechnology;
ret.visibleIdentityClasses = GetVisibleIdentityClasses(template.Identity);
}
if (template.UnitMotion)
{
ret.speed = {
"walk": getEntityValue("UnitMotion/WalkSpeed"),
};
if (template.UnitMotion.Run)
ret.speed.run = getEntityValue("UnitMotion/Run/Speed");
}
if (template.Upgrade)
{
ret.upgrades = [];
for (let upgradeName in template.Upgrade)
{
let upgrade = template.Upgrade[upgradeName];
let cost = {};
if (upgrade.Cost)
for (let res in upgrade.Cost)
cost[res] = getEntityValue("Upgrade/" + upgradeName + "/Cost/" + res, "Upgrade/Cost/" + res);
if (upgrade.Time)
cost.time = getEntityValue("Upgrade/" + upgradeName + "/Time", "Upgrade/Time");
ret.upgrades.push({
"entity": upgrade.Entity,
"tooltip": upgrade.Tooltip,
"cost": cost,
"icon": upgrade.Icon || undefined,
"requiredTechnology": upgrade.RequiredTechnology || undefined
});
}
}
if (template.ProductionQueue)
{
ret.techCostMultiplier = {};
for (let res in template.ProductionQueue.TechCostMultiplier)
ret.techCostMultiplier[res] = getEntityValue("ProductionQueue/TechCostMultiplier/" + res);
}
if (template.Trader)
ret.trader = {
"GainMultiplier": getEntityValue("Trader/GainMultiplier")
};
if (template.WallSet)
ret.wallSet = {
"templates": {
"tower": template.WallSet.Templates.Tower,
"gate": template.WallSet.Templates.Gate,
"long": template.WallSet.Templates.WallLong,
"medium": template.WallSet.Templates.WallMedium,
"short": template.WallSet.Templates.WallShort,
},
"maxTowerOverlap": +template.WallSet.MaxTowerOverlap,
"minTowerOverlap": +template.WallSet.MinTowerOverlap,
};
if (template.WallPiece)
ret.wallPiece = { "length": +template.WallPiece.Length };
return ret;
}
/**
* Get basic information about a technology template.
* @param {object} template - A valid template as obtained by loading the tech JSON file.
* @param {string} civ - Civilization for which the tech requirements should be calculated.
*/
function GetTechnologyBasicDataHelper(template, civ)
{
return {
"name": {
"generic": template.genericName
},
"icon": template.icon ? "technologies/" + template.icon : undefined,
"description": template.description,
"reqs": DeriveTechnologyRequirements(template, civ),
"modifications": template.modifications,
"affects": template.affects
};
}
/**
* Get information about a technology template.
* @param {object} template - A valid template as obtained by loading the tech JSON file.
* @param {string} civ - Civilization for which the specific name and tech requirements should be returned.
*/
function GetTechnologyDataHelper(template, civ, resources)
{
let ret = GetTechnologyBasicDataHelper(template, civ);
if (template.specificName)
ret.name.specific = template.specificName[civ] || template.specificName.generic;
ret.cost = { "time": template.researchTime ? +template.researchTime : 0 };
for (let type of resources.GetCodes())
ret.cost[type] = +(template.cost && template.cost[type] || 0);
ret.tooltip = template.tooltip;
ret.requirementsTooltip = template.requirementsTooltip || "";
return ret;
}
function calculateCarriedResources(carriedResources, tradingGoods)
{
var resources = {};
if (carriedResources)
for (let resource of carriedResources)
resources[resource.type] = (resources[resource.type] || 0) + resource.amount;
if (tradingGoods && tradingGoods.amount)
resources[tradingGoods.type] =
(resources[tradingGoods.type] || 0) +
(tradingGoods.amount.traderGain || 0) +
(tradingGoods.amount.market1Gain || 0) +
(tradingGoods.amount.market2Gain || 0);
return resources;
}
Index: ps/trunk/binaries/data/mods/public/gui/common/functions_utility.js
===================================================================
--- ps/trunk/binaries/data/mods/public/gui/common/functions_utility.js (revision 20585)
+++ ps/trunk/binaries/data/mods/public/gui/common/functions_utility.js (revision 20586)
@@ -1,260 +1,260 @@
/**
* Used for acoustic GUI notifications.
* Define the soundfile paths and specific time thresholds (avoid spam).
* And store the timestamp of last interaction for each notification.
*/
var g_SoundNotifications = {
"nick": { "soundfile": "audio/interface/ui/chat_alert.ogg", "threshold": 3000 }
};
// Get list of XML files in pathname with recursion, excepting those starting with _
function getXMLFileList(pathname)
{
- var files = Engine.BuildDirEntList(pathname, "*.xml", true);
+ var files = Engine.ListDirectoryFiles(pathname, "*.xml", true);
var result = [];
// Get only subpath from filename and discard extension
for (var i = 0; i < files.length; ++i)
{
var file = files[i];
file = file.substring(pathname.length, file.length - 4);
// Split path into directories so we can check for beginning _ character
var tokens = file.split("/");
if (tokens[tokens.length - 1][0] != "_")
result.push(file);
}
return result;
}
function getJSONFileList(pathname)
{
// Remove the path and extension from each name, since we just want the filename
- return Engine.BuildDirEntList(pathname, "*.json", false).map(
+ return Engine.ListDirectoryFiles(pathname, "*.json", false).map(
filename => filename.substring(pathname.length, filename.length - 5));
}
/**
* Returns translated history and gameplay data of all civs, optionally including a mock gaia civ.
*/
function loadCivData(selectableOnly, gaia)
{
let civData = loadCivFiles(selectableOnly);
translateObjectKeys(civData, ["Name", "Description", "History", "Special"]);
if (gaia)
civData.gaia = { "Code": "gaia", "Name": translate("Gaia") };
return deepfreeze(civData);
}
// A sorting function for arrays of objects with 'name' properties, ignoring case
function sortNameIgnoreCase(x, y)
{
let lowerX = x.name.toLowerCase();
let lowerY = y.name.toLowerCase();
if (lowerX < lowerY)
return -1;
if (lowerX > lowerY)
return 1;
return 0;
}
/**
* Escape tag start and escape characters, so users cannot use special formatting.
* Also limit string length to 256 characters (not counting escape characters).
*/
function escapeText(text, limitLength = true)
{
if (!text)
return text;
if (limitLength)
text = text.substr(0, 255);
return text.replace(/\\/g, "\\\\").replace(/\[/g, "\\[");
}
function unescapeText(text)
{
if (!text)
return text;
return text.replace(/\\\\/g, "\\").replace(/\\\[/g, "\[");
}
/**
* Merge players by team to remove duplicate Team entries, thus reducing the packet size of the lobby report.
*/
function playerDataToStringifiedTeamList(playerData)
{
let teamList = {};
for (let pData of playerData)
{
let team = pData.Team === undefined ? -1 : pData.Team;
if (!teamList[team])
teamList[team] = [];
teamList[team].push(pData);
delete teamList[team].Team;
}
return escapeText(JSON.stringify(teamList), false);
}
function stringifiedTeamListToPlayerData(stringifiedTeamList)
{
let teamList = JSON.parse(unescapeText(stringifiedTeamList));
let playerData = [];
for (let team in teamList)
for (let pData of teamList[team])
{
pData.Team = team;
playerData.push(pData);
}
return playerData;
}
function translateMapTitle(mapTitle)
{
return mapTitle == "random" ? translateWithContext("map selection", "Random") : translate(mapTitle);
}
/**
* Convert time in milliseconds to [hh:]mm:ss string representation.
* @param time Time period in milliseconds (integer)
* @return String representing time period
*/
function timeToString(time)
{
return Engine.FormatMillisecondsIntoDateStringGMT(time, time < 1000 * 60 * 60 ?
translate("mm:ss") : translate("HH:mm:ss"));
}
function removeDupes(array)
{
// loop backwards to make splice operations cheaper
let i = array.length;
while (i--)
if (array.indexOf(array[i]) != i)
array.splice(i, 1);
}
function singleplayerName()
{
return Engine.ConfigDB_GetValue("user", "playername.singleplayer") || Engine.GetSystemUsername();
}
function multiplayerName()
{
return Engine.ConfigDB_GetValue("user", "playername.multiplayer") || Engine.GetSystemUsername();
}
function tryAutoComplete(text, autoCompleteList)
{
if (!text.length)
return text;
var wordSplit = text.split(/\s/g);
if (!wordSplit.length)
return text;
var lastWord = wordSplit.pop();
if (!lastWord.length)
return text;
for (var word of autoCompleteList)
{
if (word.toLowerCase().indexOf(lastWord.toLowerCase()) != 0)
continue;
text = wordSplit.join(" ");
if (text.length > 0)
text += " ";
text += word;
break;
}
return text;
}
function autoCompleteNick(guiObject, playernames)
{
let text = guiObject.caption;
if (!text.length)
return;
let bufferPosition = guiObject.buffer_position;
let textTillBufferPosition = text.substring(0, bufferPosition);
let newText = tryAutoComplete(textTillBufferPosition, playernames);
guiObject.caption = newText + text.substring(bufferPosition);
guiObject.buffer_position = bufferPosition + (newText.length - textTillBufferPosition.length);
}
function clearChatMessages()
{
g_ChatMessages.length = 0;
Engine.GetGUIObjectByName("chatText").caption = "";
try {
for (let timer of g_ChatTimers)
clearTimeout(timer);
g_ChatTimers.length = 0;
} catch (e) {
}
}
/**
* Manage acoustic GUI notifications.
*
* @param {string} type - Notification type.
*/
function soundNotification(type)
{
if (Engine.ConfigDB_GetValue("user", "sound.notify." + type) != "true")
return;
let notificationType = g_SoundNotifications[type];
let timeNow = Date.now();
if (!notificationType.lastInteractionTime || timeNow > notificationType.lastInteractionTime + notificationType.threshold)
Engine.PlayUISound(notificationType.soundfile, false);
notificationType.lastInteractionTime = timeNow;
}
/**
* Horizontally spaces objects within a parent
*
* @param margin The gap, in px, between the objects
*/
function horizontallySpaceObjects(parentName, margin = 0)
{
let objects = Engine.GetGUIObjectByName(parentName).children;
for (let i = 0; i < objects.length; ++i)
{
let size = objects[i].size;
let width = size.right - size.left;
size.left = i * (width + margin) + margin;
size.right = (i + 1) * (width + margin);
objects[i].size = size;
}
}
/**
* Hide all children after a certain index
*/
function hideRemaining(parentName, start = 0)
{
let objects = Engine.GetGUIObjectByName(parentName).children;
for (let i = start; i < objects.length; ++i)
objects[i].hidden = true;
}
Index: ps/trunk/binaries/data/mods/public/gui/common/settings.js
===================================================================
--- ps/trunk/binaries/data/mods/public/gui/common/settings.js (revision 20585)
+++ ps/trunk/binaries/data/mods/public/gui/common/settings.js (revision 20586)
@@ -1,405 +1,405 @@
/**
* The maximum number of players that the engine supports.
* TODO: Maybe we can support more than 8 players sometime.
*/
const g_MaxPlayers = 8;
/**
* The maximum number of teams allowed.
*/
const g_MaxTeams = 4;
/**
* Directory containing all editable settings.
*/
const g_SettingsDirectory = "simulation/data/settings/";
/**
* Directory containing all biomes supported for random map scripts.
*/
const g_BiomesDirectory = "maps/random/rmbiome/biomes/";
/**
* An object containing all values given by setting name.
* Used by lobby, gamesetup, session, summary screen and replay menu.
*/
const g_Settings = loadSettingsValues();
/**
* Loads and translates all values of all settings which
* can be configured by dropdowns in the gamesetup.
*
* @returns {Object|undefined}
*/
function loadSettingsValues()
{
var settings = {
"AIDescriptions": loadAIDescriptions(),
"AIDifficulties": loadAIDifficulties(),
"Ceasefire": loadCeasefire(),
"VictoryDurations": loadVictoryDuration(),
"GameSpeeds": loadSettingValuesFile("game_speeds.json"),
"MapTypes": loadMapTypes(),
"MapSizes": loadSettingValuesFile("map_sizes.json"),
"Biomes": loadBiomes(),
"PlayerDefaults": loadPlayerDefaults(),
"PopulationCapacities": loadPopulationCapacities(),
"StartingResources": loadSettingValuesFile("starting_resources.json"),
"VictoryConditions": loadVictoryConditions()
};
if (Object.keys(settings).some(key => settings[key] === undefined))
return undefined;
return deepfreeze(settings);
}
/**
* Returns an array of objects reflecting all possible values for a given setting.
*
* @param {string} filename
* @see simulation/data/settings/
* @returns {Array|undefined}
*/
function loadSettingValuesFile(filename)
{
var json = Engine.ReadJSONFile(g_SettingsDirectory + filename);
if (!json || !json.Data)
{
error("Could not load " + filename + "!");
return undefined;
}
if (json.TranslatedKeys)
{
let keyContext = json.TranslatedKeys;
if (json.TranslationContext)
{
keyContext = {};
for (let key of json.TranslatedKeys)
keyContext[key] = json.TranslationContext;
}
translateObjectKeys(json.Data, keyContext);
}
return json.Data;
}
/**
* Loads the descriptions as defined in simulation/ai/.../data.json and loaded by ICmpAIManager.cpp.
*
* @returns {Array}
*/
function loadAIDescriptions()
{
var ais = Engine.GetAIs();
translateObjectKeys(ais, ["name", "description"]);
return ais.sort((a, b) => a.data.name.localeCompare(b.data.name));
}
/**
* Hardcoded, as modding is not supported without major changes.
* Notice the AI code parses the difficulty level by the index, not by name.
*
* @returns {Array}
*/
function loadAIDifficulties()
{
return [
{
"Name": "sandbox",
"Title": translateWithContext("aiDiff", "Sandbox")
},
{
"Name": "very easy",
"Title": translateWithContext("aiDiff", "Very Easy")
},
{
"Name": "easy",
"Title": translateWithContext("aiDiff", "Easy")
},
{
"Name": "medium",
"Title": translateWithContext("aiDiff", "Medium"),
"Default": true
},
{
"Name": "hard",
"Title": translateWithContext("aiDiff", "Hard")
},
{
"Name": "very hard",
"Title": translateWithContext("aiDiff", "Very Hard")
}
];
}
/**
* Loads available victory times for victory conditions like Wonder and Capture the Relic.
*/
function loadVictoryDuration()
{
var jsonFile = "victory_times.json";
var json = Engine.ReadJSONFile(g_SettingsDirectory + jsonFile);
if (!json || json.Default === undefined || !json.Times || !Array.isArray(json.Times))
{
error("Could not load " + jsonFile);
return undefined;
}
return json.Times.map(duration => ({
"Duration": duration,
"Default": duration == json.Default,
"Title": sprintf(translatePluralWithContext("victory duration", "%(min)s minute", "%(min)s minutes", duration), { "min": duration })
}));
}
/**
* Loads available ceasefire settings.
*
* @returns {Array|undefined}
*/
function loadCeasefire()
{
var json = Engine.ReadJSONFile(g_SettingsDirectory + "ceasefire.json");
if (!json || json.Default === undefined || !json.Times || !Array.isArray(json.Times))
{
error("Could not load ceasefire.json");
return undefined;
}
return json.Times.map(timeout => ({
"Duration": timeout,
"Default": timeout == json.Default,
"Title": timeout == 0 ? translateWithContext("ceasefire", "No ceasefire") :
sprintf(translatePluralWithContext("ceasefire", "%(minutes)s minute", "%(minutes)s minutes", timeout), { "minutes": timeout })
}));
}
/**
* Hardcoded, as modding is not supported without major changes.
*
* @returns {Array}
*/
function loadMapTypes()
{
return [
{
"Name": "skirmish",
"Title": translateWithContext("map", "Skirmish"),
"Description": translate("A map with a predefined landscape and number of players. Freely select the other gamesettings."),
"Default": true
},
{
"Name": "random",
"Title": translateWithContext("map", "Random"),
"Description": translate("Create a unique map with a different resource distribution each time. Freely select the number of players and teams.")
},
{
"Name": "scenario",
"Title": translateWithContext("map", "Scenario"),
"Description": translate("A map with a predefined landscape and matchsettings.")
}
];
}
function loadBiomes()
{
- return Engine.BuildDirEntList(g_BiomesDirectory, "*.json", false).map(file => {
+ return Engine.ListDirectoryFiles(g_BiomesDirectory, "*.json", false).map(file => {
let description = Engine.ReadJSONFile(file).Description;
return {
"Id": file.substr(g_BiomesDirectory.length).slice(0, -".json".length),
"Title": translateWithContext("biome definition", description.Title),
"Description": translateWithContext("biome definition", description.Description)
};
});
}
/**
* Loads available gametypes.
*
* @returns {Array|undefined}
*/
function loadVictoryConditions()
{
let subdir = "victory_conditions/";
- let files = Engine.BuildDirEntList(g_SettingsDirectory + subdir, "*.json", false).map(
+ let files = Engine.ListDirectoryFiles(g_SettingsDirectory + subdir, "*.json", false).map(
file => file.substr(g_SettingsDirectory.length));
let victoryConditions = files.map(file => {
let vc = loadSettingValuesFile(file);
if (vc)
vc.Name = file.substr(subdir.length, file.length - (subdir + ".json").length);
return vc;
});
if (victoryConditions.some(vc => vc == undefined))
return undefined;
// TODO: We might support enabling victory conditions separately sometime.
// Until then, we supplement the endless gametype here.
victoryConditions.push({
"Name": "endless",
"Title": translateWithContext("victory condition", "None"),
"Description": translate("Endless game."),
"Scripts": []
});
return victoryConditions;
}
/**
* Loads the default player settings (like civs and colors).
*
* @returns {Array|undefined}
*/
function loadPlayerDefaults()
{
var json = Engine.ReadJSONFile(g_SettingsDirectory + "player_defaults.json");
if (!json || !json.PlayerData)
{
error("Could not load player_defaults.json");
return undefined;
}
return json.PlayerData;
}
/**
* Loads available population capacities.
*
* @returns {Array|undefined}
*/
function loadPopulationCapacities()
{
var json = Engine.ReadJSONFile(g_SettingsDirectory + "population_capacities.json");
if (!json || json.Default === undefined || !json.PopulationCapacities || !Array.isArray(json.PopulationCapacities))
{
error("Could not load population_capacities.json");
return undefined;
}
return json.PopulationCapacities.map(population => ({
"Population": population,
"Default": population == json.Default,
"Title": population < 10000 ? population : translate("Unlimited")
}));
}
/**
* Creates an object with all values of that property of the given setting and
* finds the index of the default value.
*
* This allows easy copying of setting values to dropdown lists.
*
* @param {Array} settingValues
* @returns {Object|undefined}
*/
function prepareForDropdown(settingValues)
{
if (!settingValues)
return undefined;
let settings = { "Default": 0 };
for (let index in settingValues)
{
for (let property in settingValues[index])
{
if (property == "Default")
continue;
if (!settings[property])
settings[property] = [];
// Switch property and index
settings[property][index] = settingValues[index][property];
}
// Copy default value
if (settingValues[index].Default)
settings.Default = +index;
}
return deepfreeze(settings);
}
function getGameSpeedChoices(allowFastForward)
{
return prepareForDropdown(g_Settings.GameSpeeds.filter(speed => !speed.FastForward || allowFastForward));
}
/**
* Returns title or placeholder.
*
* @param {string} aiName - for example "petra"
*/
function translateAIName(aiName)
{
let description = g_Settings.AIDescriptions.find(ai => ai.id == aiName);
return description ? translate(description.data.name) : translateWithContext("AI name", "Unknown");
}
/**
* Returns title or placeholder.
*
* @param {Number} index - index of AIDifficulties
*/
function translateAIDifficulty(index)
{
let difficulty = g_Settings.AIDifficulties[index];
return difficulty ? difficulty.Title : translateWithContext("AI difficulty", "Unknown");
}
/**
* Returns title or placeholder.
*
* @param {string} mapType - for example "skirmish"
* @returns {string}
*/
function translateMapType(mapType)
{
let type = g_Settings.MapTypes.find(t => t.Name == mapType);
return type ? type.Title : translateWithContext("map type", "Unknown");
}
/**
* Returns title or placeholder "Default".
*
* @param {Number} mapSize - tilecount
* @returns {string}
*/
function translateMapSize(tiles)
{
let mapSize = g_Settings.MapSizes.find(size => size.Tiles == +tiles);
return mapSize ? mapSize.Name : translateWithContext("map size", "Default");
}
/**
* Returns title or placeholder.
*
* @param {Number} population - for example 300
* @returns {string}
*/
function translatePopulationCapacity(population)
{
let popCap = g_Settings.PopulationCapacities.find(p => p.Population == population);
return popCap ? popCap.Title : translateWithContext("population capacity", "Unknown");
}
/**
* Returns title or placeholder.
*
* @param {string} gameType - for example "conquest"
* @returns {string}
*/
function translateVictoryCondition(gameType)
{
let victoryCondition = g_Settings.VictoryConditions.find(vc => vc.Name == gameType);
return victoryCondition ? victoryCondition.Title : translateWithContext("victory condition", "Unknown");
}
Index: ps/trunk/binaries/data/mods/public/gui/loading/loading.js
===================================================================
--- ps/trunk/binaries/data/mods/public/gui/loading/loading.js (revision 20585)
+++ ps/trunk/binaries/data/mods/public/gui/loading/loading.js (revision 20586)
@@ -1,105 +1,105 @@
var g_Data;
var g_EndPieceWidth = 16;
function init(data)
{
g_Data = data;
Engine.SetCursor("cursor-wait");
// Get tip image and corresponding tip text
- let tipTextLoadingArray = Engine.BuildDirEntList("gui/text/tips/", "*.txt", false);
+ let tipTextLoadingArray = Engine.ListDirectoryFiles("gui/text/tips/", "*.txt", false);
if (tipTextLoadingArray.length > 0)
{
// Set tip text
let tipTextFilePath = pickRandom(tipTextLoadingArray);
let tipText = Engine.TranslateLines(Engine.ReadFile(tipTextFilePath));
if (tipText)
{
let index = tipText.indexOf("\n");
let tipTextTitle = tipText.substring(0, index);
let tipTextMessage = tipText.substring(index);
Engine.GetGUIObjectByName("tipTitle").caption = tipTextTitle ? tipTextTitle : "";
Engine.GetGUIObjectByName("tipText").caption = tipTextMessage ? tipTextMessage : "";
}
// Set tip image
let fileName = tipTextFilePath.substring(tipTextFilePath.lastIndexOf("/") + 1).replace(".txt", ".png");
let tipImageFilePath = "loading/tips/" + fileName;
let sprite = "stretched:" + tipImageFilePath;
Engine.GetGUIObjectByName("tipImage").sprite = sprite ? sprite : "";
}
else
error("Failed to find any matching tips for the loading screen.");
// janwas: main loop now sets progress / description, but that won't
// happen until the first timeslice completes, so set initial values.
let loadingMapName = Engine.GetGUIObjectByName("loadingMapName");
if (data)
{
let mapName = translate(data.attribs.settings.Name);
switch (data.attribs.mapType)
{
case "skirmish":
case "scenario":
loadingMapName.caption = sprintf(translate("Loading ā%(map)sā"), { "map": mapName });
break;
case "random":
loadingMapName.caption = sprintf(translate("Generating ā%(map)sā"), { "map": mapName });
break;
default:
error("Unknown map type: " + data.attribs.mapType);
}
}
Engine.GetGUIObjectByName("progressText").caption = "";
Engine.GetGUIObjectByName("progressbar").caption = 0;
// Pick a random quote of the day (each line is a separate tip).
let quoteArray = Engine.ReadFileLines("gui/text/quotes.txt").filter(line => line);
Engine.GetGUIObjectByName("quoteText").caption = translate(pickRandom(quoteArray));
}
function displayProgress()
{
// Make the progessbar finish a little early so that the user can actually see it finish
if (g_Progress >= 100)
return;
// Show 100 when it is really 99
let progress = g_Progress + 1;
Engine.GetGUIObjectByName("progressbar").caption = progress; // display current progress
Engine.GetGUIObjectByName("progressText").caption = progress + "%";
// Displays detailed loading info rather than a percent
// Engine.GetGUIObjectByName("progressText").caption = g_LoadDescription; // display current progess details
// Keep curved right edge of progress bar in sync with the rest of the progress bar
let middle = Engine.GetGUIObjectByName("progressbar");
let rightSide = Engine.GetGUIObjectByName("progressbar_right");
let middleLength = (middle.size.right - middle.size.left) - (g_EndPieceWidth / 2);
let increment = Math.round(progress * middleLength / 100);
let size = rightSide.size;
size.left = increment;
size.right = increment + g_EndPieceWidth;
rightSide.size = size;
}
/**
* This is a reserved function name that is executed by the engine when it is ready
* to start the game (i.e. loading progress has reached 100%).
*/
function reallyStartGame()
{
Engine.SwitchGuiPage("page_session.xml", g_Data);
Engine.ResetCursor();
}
Index: ps/trunk/binaries/data/mods/public/gui/reference/common/load.js
===================================================================
--- ps/trunk/binaries/data/mods/public/gui/reference/common/load.js (revision 20585)
+++ ps/trunk/binaries/data/mods/public/gui/reference/common/load.js (revision 20586)
@@ -1,346 +1,346 @@
/**
* Paths to certain files.
*/
const g_TechnologyPath = "simulation/data/technologies/";
const g_AuraPath = "simulation/data/auras/";
/**
* Raw Data Caches.
*/
var g_AuraData = {};
var g_TemplateData = {};
var g_TechnologyData = {};
var g_CivData = loadCivData(true, false);
/**
* Parsed Data Stores.
*/
var g_ParsedData = {};
var g_ResourceData = new Resources();
var g_DamageTypes = new DamageTypes();
// This must be defined after the g_TechnologyData cache object is declared.
var g_AutoResearchTechList = findAllAutoResearchedTechs();
/**
* Loads raw entity template.
*
* Loads from local cache if data present, else from file system.
*
* @param {string} templateName
* @return {object} Object containing raw template data.
*/
function loadTemplate(templateName)
{
if (!(templateName in g_TemplateData))
{
// We need to clone the template because we want to perform some translations.
let data = clone(Engine.GetTemplate(templateName));
translateObjectKeys(data, ["GenericName", "SpecificName", "Tooltip"]);
if (data.Auras)
for (let auraID of data.Auras._string.split(/\s+/))
loadAuraData(auraID);
g_TemplateData[templateName] = data;
}
return g_TemplateData[templateName];
}
/**
* Loads raw technology template.
*
* Loads from local cache if available, else from file system.
*
* @param {string} templateName
* @return {object} Object containing raw template data.
*/
function loadTechData(templateName)
{
if (!(templateName in g_TechnologyData))
{
let data = Engine.ReadJSONFile(g_TechnologyPath + templateName + ".json");
translateObjectKeys(data, ["genericName", "tooltip"]);
g_TechnologyData[templateName] = data;
}
return g_TechnologyData[templateName];
}
function techDataExists(templateName)
{
return Engine.FileExists("simulation/data/technologies/" + templateName + ".json");
}
/**
* Loads raw aura template.
*
* Loads from local cache if available, else from file system.
*
* @param {string} templateName
* @return {object} Object containing raw template data.
*/
function loadAuraData(templateName)
{
if (!(templateName in g_AuraData))
{
let data = Engine.ReadJSONFile(g_AuraPath + templateName + ".json");
translateObjectKeys(data, ["auraName", "auraDescription"]);
g_AuraData[templateName] = data;
}
return g_AuraData[templateName];
}
/**
* Load and parse unit from entity template.
*
* @param {string} templateName
* @return Sanitized object about the requested unit or null if entity template doesn't exist.
*/
function loadUnit(templateName)
{
if (!Engine.TemplateExists(templateName))
return null;
let template = loadTemplate(templateName);
let unit = GetTemplateDataHelper(template, null, g_AuraData, g_ResourceData, g_DamageTypes, g_CurrentModifiers);
if (template.ProductionQueue)
{
unit.production = {};
if (template.ProductionQueue.Entities)
{
unit.production.units = [];
for (let build of template.ProductionQueue.Entities._string.split(" "))
{
build = build.replace("{civ}", g_SelectedCiv);
if (Engine.TemplateExists(build))
unit.production.units.push(build);
}
}
if (template.ProductionQueue.Technologies)
{
unit.production.techs = [];
for (let research of template.ProductionQueue.Technologies._string.split(" "))
{
if (research.indexOf("{civ}") != -1)
{
let civResearch = research.replace("{civ}", g_SelectedCiv);
research = techDataExists(civResearch) ?
civResearch : research.replace("{civ}", "generic");
}
if (isPairTech(research))
for (let tech of loadTechnologyPair(research).techs)
unit.production.techs.push(tech);
else
unit.production.techs.push(research);
}
}
}
if (template.Builder && template.Builder.Entities._string)
{
unit.builder = [];
for (let build of template.Builder.Entities._string.split(" "))
{
build = build.replace("{civ}", g_SelectedCiv);
if (Engine.TemplateExists(build))
unit.builder.push(build);
}
}
if (unit.upgrades)
unit.upgrades = getActualUpgradeData(unit.upgrades);
return unit;
}
/**
* Load and parse structure from entity template.
*
* @param {string} templateName
* @return {object} Sanitized data about the requested structure or null if entity template doesn't exist.
*/
function loadStructure(templateName)
{
if (!Engine.TemplateExists(templateName))
return null;
let template = loadTemplate(templateName);
let structure = GetTemplateDataHelper(template, null, g_AuraData, g_ResourceData, g_DamageTypes, g_CurrentModifiers);
structure.production = {
"technology": [],
"units": []
};
if (template.ProductionQueue)
{
if (template.ProductionQueue.Entities && template.ProductionQueue.Entities._string)
for (let build of template.ProductionQueue.Entities._string.split(" "))
{
build = build.replace("{civ}", g_SelectedCiv);
if (Engine.TemplateExists(build))
structure.production.units.push(build);
}
if (template.ProductionQueue.Technologies && template.ProductionQueue.Technologies._string)
for (let research of template.ProductionQueue.Technologies._string.split(" "))
{
if (research.indexOf("{civ}") != -1)
{
let civResearch = research.replace("{civ}", g_SelectedCiv);
research = techDataExists(civResearch) ?
civResearch : research.replace("{civ}", "generic");
}
if (isPairTech(research))
for (let tech of loadTechnologyPair(research).techs)
structure.production.technology.push(tech);
else
structure.production.technology.push(research);
}
}
if (structure.upgrades)
structure.upgrades = getActualUpgradeData(structure.upgrades);
if (structure.wallSet)
{
structure.wallset = {};
if (!structure.upgrades)
structure.upgrades = [];
// Note: An assumption is made here that wall segments all have the same armor and auras
let struct = loadStructure(structure.wallSet.templates.long);
structure.armour = struct.armour;
structure.auras = struct.auras;
// For technology cost multiplier, we need to use the tower
struct = loadStructure(structure.wallSet.templates.tower);
structure.techCostMultiplier = struct.techCostMultiplier;
let health;
for (let wSegm in structure.wallSet.templates)
{
let wPart = loadStructure(structure.wallSet.templates[wSegm]);
structure.wallset[wSegm] = wPart;
for (let research of wPart.production.technology)
structure.production.technology.push(research);
if (wPart.upgrades)
structure.upgrades = structure.upgrades.concat(wPart.upgrades);
if (["gate", "tower"].indexOf(wSegm) != -1)
continue;
if (!health)
{
health = { "min": wPart.health, "max": wPart.health };
continue;
}
health.min = Math.min(health.min, wPart.health);
health.max = Math.max(health.max, wPart.health);
}
if (health.min == health.max)
structure.health = health.min;
else
structure.health = sprintf(translate("%(health_min)s to %(health_max)s"), {
"health_min": health.min,
"health_max": health.max
});
}
return structure;
}
/**
* Load and parse technology from json template.
*
* @param {string} templateName
* @return {object} Sanitized data about the requested technology.
*/
function loadTechnology(techName)
{
let template = loadTechData(techName);
let tech = GetTechnologyDataHelper(template, g_SelectedCiv, g_ResourceData);
if (template.pair !== undefined)
{
tech.pair = template.pair;
tech.reqs = mergeRequirements(tech.reqs, loadTechnologyPair(template.pair).reqs);
}
return tech;
}
/**
* Crudely iterates through every tech JSON file and identifies those
* that are auto-researched.
*
* @return {array} List of techs that are researched automatically
*/
function findAllAutoResearchedTechs()
{
let techList = [];
- for (let filename of Engine.BuildDirEntList(g_TechnologyPath, "*.json", true))
+ for (let filename of Engine.ListDirectoryFiles(g_TechnologyPath, "*.json", true))
{
// -5 to strip off the file extension
let templateName = filename.slice(g_TechnologyPath.length, -5);
let data = loadTechData(templateName);
if (data && data.autoResearch)
techList.push(templateName);
}
return techList;
}
/**
* @param {string} phaseCode
* @return {object} Sanitized object containing phase data
*/
function loadPhase(phaseCode)
{
let template = loadTechData(phaseCode);
let phase = loadTechnology(phaseCode);
phase.actualPhase = phaseCode;
if (template.replaces !== undefined)
phase.actualPhase = template.replaces[0];
return phase;
}
/**
* @param {string} pairCode
* @return {object} Contains a list and the requirements of the techs in the pair
*/
function loadTechnologyPair(pairCode)
{
var pairInfo = loadTechData(pairCode);
return {
"techs": [ pairInfo.top, pairInfo.bottom ],
"reqs": DeriveTechnologyRequirements(pairInfo, g_SelectedCiv)
};
}
/**
* @param {string} modCode
* @return {object} Sanitized object containing modifier tech data
*/
function loadModifierTech(modCode)
{
if (!Engine.FileExists("simulation/data/technologies/"+modCode+".json"))
return {};
return loadTechData(modCode);
}
Index: ps/trunk/binaries/data/mods/public/simulation/components/DataTemplateManager.js
===================================================================
--- ps/trunk/binaries/data/mods/public/simulation/components/DataTemplateManager.js (revision 20585)
+++ ps/trunk/binaries/data/mods/public/simulation/components/DataTemplateManager.js (revision 20586)
@@ -1,68 +1,71 @@
/**
* System component which loads the technology and the aura data files
*/
function DataTemplateManager() {}
DataTemplateManager.prototype.Schema =
"";
DataTemplateManager.prototype.Init = function()
{
+ this.technologiesPath = "simulation/data/technologies/";
+ this.aurasPath = "simulation/data/auras/";
+
this.allTechs = {};
this.allAuras = {};
for (let techName of this.ListAllTechs())
this.GetTechnologyTemplate(techName);
for (let auraName of this.ListAllAuras())
this.GetAuraTemplate(auraName);
deepfreeze(this.allTechs);
deepfreeze(this.allAuras);
};
DataTemplateManager.prototype.GetTechnologyTemplate = function(template)
{
if (!this.allTechs[template])
{
- this.allTechs[template] = Engine.ReadJSONFile("simulation/data/technologies/" + template + ".json");
+ this.allTechs[template] = Engine.ReadJSONFile(this.technologiesPath + template + ".json");
if (!this.allTechs[template])
error("Failed to load technology \"" + template + "\"");
}
return this.allTechs[template];
};
DataTemplateManager.prototype.GetAuraTemplate = function(template)
{
if (!this.allAuras[template])
{
- this.allAuras[template] = Engine.ReadJSONFile("simulation/data/auras/" + template + ".json");
+ this.allAuras[template] = Engine.ReadJSONFile(this.aurasPath + template + ".json");
if (!this.allAuras[template])
error("Failed to load aura \"" + template + "\"");
}
return this.allAuras[template];
};
DataTemplateManager.prototype.ListAllTechs = function()
{
- return Engine.FindJSONFiles("technologies", true);
+ return Engine.ListDirectoryFiles(this.technologiesPath, "*.json", true).map(file => file.slice(this.technologiesPath.length, -".json".length));
};
DataTemplateManager.prototype.ListAllAuras = function()
{
- return Engine.FindJSONFiles("auras", true);
+ return Engine.ListDirectoryFiles(this.aurasPath, "*.json", true).map(file => file.slice(this.aurasPath.length, -".json".length));
};
DataTemplateManager.prototype.GetAllTechs = function()
{
return this.allTechs;
};
DataTemplateManager.prototype.TechFileExists = function(template)
{
return Engine.DataFileExists("technologies/" + template + ".json");
};
Engine.RegisterSystemComponentType(IID_DataTemplateManager, "DataTemplateManager", DataTemplateManager);
Index: ps/trunk/source/ps/scripting/JSInterface_VFS.cpp
===================================================================
--- ps/trunk/source/ps/scripting/JSInterface_VFS.cpp (revision 20585)
+++ ps/trunk/source/ps/scripting/JSInterface_VFS.cpp (revision 20586)
@@ -1,271 +1,272 @@
/* Copyright (C) 2017 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see .
*/
#include "precompiled.h"
#include
#include "ps/CLogger.h"
#include "ps/CStr.h"
#include "ps/Filesystem.h"
#include "scriptinterface/ScriptVal.h"
#include "scriptinterface/ScriptInterface.h"
#include "ps/scripting/JSInterface_VFS.h"
#include "lib/file/vfs/vfs_util.h"
// Only allow engine compartments to read files they may be concerned about.
#define PathRestriction_GUI {L""}
#define PathRestriction_Simulation {L"simulation/"}
#define PathRestriction_Maps {L"simulation/", L"maps/"}
// shared error handling code
#define JS_CHECK_FILE_ERR(err)\
/* this is liable to happen often, so don't complain */\
if (err == ERR::VFS_FILE_NOT_FOUND)\
{\
return 0; \
}\
/* unknown failure. We output an error message. */\
else if (err < 0)\
LOGERROR("Unknown failure in VFS %i", err );
/* else: success */
// state held across multiple BuildDirEntListCB calls; init by BuildDirEntList.
struct BuildDirEntListState
{
JSContext* cx;
JS::PersistentRootedObject filename_array;
int cur_idx;
BuildDirEntListState(JSContext* cx_)
: cx(cx_),
filename_array(cx, JS_NewArrayObject(cx, JS::HandleValueArray::empty())),
cur_idx(0)
{
}
};
// called for each matching directory entry; add its full pathname to array.
static Status BuildDirEntListCB(const VfsPath& pathname, const CFileInfo& UNUSED(fileINfo), uintptr_t cbData)
{
BuildDirEntListState* s = (BuildDirEntListState*)cbData;
JSAutoRequest rq(s->cx);
JS::RootedObject filenameArrayObj(s->cx, s->filename_array);
JS::RootedValue val(s->cx);
ScriptInterface::ToJSVal( s->cx, &val, CStrW(pathname.string()) );
JS_SetElement(s->cx, filenameArrayObj, s->cur_idx++, val);
return INFO::OK;
}
// Return an array of pathname strings, one for each matching entry in the
// specified directory.
-//
-// pathnames = buildDirEntList(start_path [, filter_string [, recursive ] ]);
-// directory: VFS path
// filter_string: default "" matches everything; otherwise, see vfs_next_dirent.
// recurse: should subdirectories be included in the search? default false.
-//
-// note: full pathnames of each file/subdirectory are returned,
-// ready for use as a "filename" for the other functions.
-JS::Value JSI_VFS::BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& path, const std::wstring& filterStr, bool recurse)
+JS::Value JSI_VFS::BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& validPaths, const std::wstring& path, const std::wstring& filterStr, bool recurse)
{
+ if (!PathRestrictionMet(pCxPrivate, validPaths, path))
+ return JS::NullValue();
+
// convert to const wchar_t*; if there's no filter, pass 0 for speed
// (interpreted as: "accept all files without comparing").
const wchar_t* filter = 0;
if (!filterStr.empty())
filter = filterStr.c_str();
int flags = recurse ? vfs::DIR_RECURSIVE : 0;
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
JSAutoRequest rq(cx);
// build array in the callback function
BuildDirEntListState state(cx);
vfs::ForEachFile(g_VFS, path, BuildDirEntListCB, (uintptr_t)&state, filter, flags);
return OBJECT_TO_JSVAL(state.filename_array);
}
// Return true iff the file exits
bool JSI_VFS::FileExists(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const CStrW& filename)
{
return (g_VFS->GetFileInfo(filename, 0) == INFO::OK);
}
// Return time [seconds since 1970] of the last modification to the specified file.
double JSI_VFS::GetFileMTime(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& filename)
{
CFileInfo fileInfo;
Status err = g_VFS->GetFileInfo(filename, &fileInfo);
JS_CHECK_FILE_ERR(err);
return (double)fileInfo.MTime();
}
// Return current size of file.
unsigned int JSI_VFS::GetFileSize(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& filename)
{
CFileInfo fileInfo;
Status err = g_VFS->GetFileInfo(filename, &fileInfo);
JS_CHECK_FILE_ERR(err);
return (unsigned int)fileInfo.Size();
}
// Return file contents in a string. Assume file is UTF-8 encoded text.
JS::Value JSI_VFS::ReadFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename)
{
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
JSAutoRequest rq(cx);
CVFSFile file;
if (file.Load(g_VFS, filename) != PSRETURN_OK)
return JS::NullValue();
CStr contents = file.DecodeUTF8(); // assume it's UTF-8
// Fix CRLF line endings. (This function will only ever be used on text files.)
contents.Replace("\r\n", "\n");
// Decode as UTF-8
JS::RootedValue ret(cx);
ScriptInterface::ToJSVal(cx, &ret, contents.FromUTF8());
return ret;
}
// Return file contents as an array of lines. Assume file is UTF-8 encoded text.
JS::Value JSI_VFS::ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename)
{
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
JSAutoRequest rq(cx);
CVFSFile file;
if (file.Load(g_VFS, filename) != PSRETURN_OK)
return JSVAL_NULL;
CStr contents = file.DecodeUTF8(); // assume it's UTF-8
// Fix CRLF line endings. (This function will only ever be used on text files.)
contents.Replace("\r\n", "\n");
// split into array of strings (one per line)
std::stringstream ss(contents);
JS::RootedObject line_array(cx, JS_NewArrayObject(cx, JS::HandleValueArray::empty()));
std::string line;
int cur_line = 0;
while (std::getline(ss, line))
{
// Decode each line as UTF-8
JS::RootedValue val(cx);
ScriptInterface::ToJSVal(cx, &val, CStr(line).FromUTF8());
JS_SetElement(cx, line_array, cur_line++, val);
}
return JS::ObjectValue(*line_array);
}
JS::Value JSI_VFS::ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& validPaths, const CStrW& filePath)
{
if (!PathRestrictionMet(pCxPrivate, validPaths, filePath))
return JS::NullValue();
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
JSAutoRequest rq(cx);
JS::RootedValue out(cx);
pCxPrivate->pScriptInterface->ReadJSONFile(filePath, &out);
return out;
}
void JSI_VFS::WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath, JS::HandleValue val1)
{
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
JSAutoRequest rq(cx);
// TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON.
JS::RootedValue val(cx, val1);
std::string str(pCxPrivate->pScriptInterface->StringifyJSON(&val, false));
VfsPath path(filePath);
WriteBuffer buf;
buf.Append(str.c_str(), str.length());
g_VFS->CreateFile(path, buf.Data(), buf.Size());
}
bool JSI_VFS::PathRestrictionMet(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& validPaths, const CStrW& filePath)
{
for (const CStrW& validPath : validPaths)
if (filePath.find(validPath) == 0)
return true;
CStrW allowedPaths;
for (std::size_t i = 0; i < validPaths.size(); ++i)
{
if (i != 0)
allowedPaths += L", ";
allowedPaths += L"\"" + validPaths[i] + L"\"";
}
JS_ReportError(
pCxPrivate->pScriptInterface->GetContext(),
"This part of the engine may only read from %s!",
utf8_from_wstring(allowedPaths).c_str());
return false;
}
#define VFS_ScriptFunctions(context)\
JS::Value Script_ReadJSONFile_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath)\
{\
return JSI_VFS::ReadJSONFile(pCxPrivate, PathRestriction_##context, filePath);\
-}
+}\
+JS::Value Script_ListDirectoryFiles_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& path, const std::wstring& filterStr, bool recurse)\
+{\
+ return JSI_VFS::BuildDirEntList(pCxPrivate, PathRestriction_##context, path, filterStr, recurse);\
+}\
VFS_ScriptFunctions(GUI);
VFS_ScriptFunctions(Simulation);
VFS_ScriptFunctions(Maps);
-
#undef VFS_ScriptFunctions
void JSI_VFS::RegisterScriptFunctions_GUI(const ScriptInterface& scriptInterface)
{
- scriptInterface.RegisterFunction("BuildDirEntList");
+ scriptInterface.RegisterFunction("ListDirectoryFiles");
scriptInterface.RegisterFunction("FileExists");
scriptInterface.RegisterFunction("GetFileMTime");
scriptInterface.RegisterFunction("GetFileSize");
scriptInterface.RegisterFunction("ReadFile");
scriptInterface.RegisterFunction("ReadFileLines");
scriptInterface.RegisterFunction("ReadJSONFile");
scriptInterface.RegisterFunction("WriteJSONFile");
}
void JSI_VFS::RegisterScriptFunctions_Simulation(const ScriptInterface& scriptInterface)
{
+ scriptInterface.RegisterFunction("ListDirectoryFiles");
scriptInterface.RegisterFunction("ReadJSONFile");
}
void JSI_VFS::RegisterScriptFunctions_Maps(const ScriptInterface& scriptInterface)
{
- scriptInterface.RegisterFunction("BuildDirEntList");
+ scriptInterface.RegisterFunction("ListDirectoryFiles");
scriptInterface.RegisterFunction("FileExists");
scriptInterface.RegisterFunction("ReadJSONFile");
}
Index: ps/trunk/source/ps/scripting/JSInterface_VFS.h
===================================================================
--- ps/trunk/source/ps/scripting/JSInterface_VFS.h (revision 20585)
+++ ps/trunk/source/ps/scripting/JSInterface_VFS.h (revision 20586)
@@ -1,72 +1,64 @@
/* Copyright (C) 2017 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see .
*/
// JSInterface_VFS.h
//
// The JavaScript wrapper around useful snippets of the VFS
#ifndef INCLUDED_JSI_VFS
#define INCLUDED_JSI_VFS
#include "scriptinterface/ScriptInterface.h"
// these are registered in ScriptFunctions.cpp, hence the need for a header.
namespace JSI_VFS
{
// Return an array of pathname strings, one for each matching entry in the
// specified directory.
- //
- // pathnames = buildDirEntList(start_path [, filter_string [, recursive ] ]);
- // directory: VFS path
- // filter_string: see match_wildcard; "" matches everything.
- // recurse: should subdirectories be included in the search? default false.
- //
- // note: full pathnames of each file/subdirectory are returned,
- // ready for use as a "filename" for the other functions.
- JS::Value BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& path, const std::wstring& filterStr, bool recurse);
+ JS::Value BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& validPaths, const std::wstring& path, const std::wstring& filterStr, bool recurse);
// Return true iff the file exists
bool FileExists(ScriptInterface::CxPrivate* pCxPrivate, const CStrW& filename);
// Return time [seconds since 1970] of the last modification to the specified file.
double GetFileMTime(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename);
// Return current size of file.
unsigned int GetFileSize(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename);
// Return file contents in a string.
JS::Value ReadFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename);
// Return file contents as an array of lines.
JS::Value ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename);
// Return file contents parsed as a JS Object
JS::Value ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& validPaths, const CStrW& filePath);
// Save given JS Object to a JSON file
void WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath, JS::HandleValue val1);
// Tests whether the current script context is allowed to read from the given directory
bool PathRestrictionMet(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& validPaths, const CStrW& filePath);
void RegisterScriptFunctions_GUI(const ScriptInterface& scriptInterface);
void RegisterScriptFunctions_Simulation(const ScriptInterface& scriptInterface);
void RegisterScriptFunctions_Maps(const ScriptInterface& scriptInterface);
}
#endif
Index: ps/trunk/source/simulation2/system/ComponentManager.cpp
===================================================================
--- ps/trunk/source/simulation2/system/ComponentManager.cpp (revision 20585)
+++ ps/trunk/source/simulation2/system/ComponentManager.cpp (revision 20586)
@@ -1,1228 +1,1192 @@
/* Copyright (C) 2017 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see .
*/
#include "precompiled.h"
#include "ComponentManager.h"
#include "DynamicSubscription.h"
#include "IComponent.h"
#include "ParamNode.h"
#include "SimContext.h"
#include "simulation2/MessageTypes.h"
#include "simulation2/components/ICmpTemplateManager.h"
#include "lib/utf8.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "ps/scripting/JSInterface_VFS.h"
/**
* Used for script-only message types.
*/
class CMessageScripted : public CMessage
{
public:
virtual int GetType() const { return mtid; }
virtual const char* GetScriptHandlerName() const { return handlerName.c_str(); }
virtual const char* GetScriptGlobalHandlerName() const { return globalHandlerName.c_str(); }
virtual JS::Value ToJSVal(const ScriptInterface& UNUSED(scriptInterface)) const { return msg.get(); }
CMessageScripted(const ScriptInterface& scriptInterface, int mtid, const std::string& name, JS::HandleValue msg) :
mtid(mtid), handlerName("On" + name), globalHandlerName("OnGlobal" + name), msg(scriptInterface.GetJSRuntime(), msg)
{
}
int mtid;
std::string handlerName;
std::string globalHandlerName;
JS::PersistentRootedValue msg;
};
CComponentManager::CComponentManager(CSimContext& context, shared_ptr rt, bool skipScriptFunctions) :
m_NextScriptComponentTypeId(CID__LastNative),
m_ScriptInterface("Engine", "Simulation", rt),
m_SimContext(context), m_CurrentlyHotloading(false)
{
context.SetComponentManager(this);
m_ScriptInterface.SetCallbackData(static_cast (this));
m_ScriptInterface.ReplaceNondeterministicRNG(m_RNG);
m_ScriptInterface.LoadGlobalScripts();
// For component script tests, the test system sets up its own scripted implementation of
// these functions, so we skip registering them here in those cases
if (!skipScriptFunctions)
{
JSI_VFS::RegisterScriptFunctions_Simulation(m_ScriptInterface);
m_ScriptInterface.RegisterFunction ("RegisterComponentType");
m_ScriptInterface.RegisterFunction ("RegisterSystemComponentType");
m_ScriptInterface.RegisterFunction ("ReRegisterComponentType");
m_ScriptInterface.RegisterFunction ("RegisterInterface");
m_ScriptInterface.RegisterFunction ("RegisterMessageType");
m_ScriptInterface.RegisterFunction ("RegisterGlobal");
m_ScriptInterface.RegisterFunction ("QueryInterface");
m_ScriptInterface.RegisterFunction, int, CComponentManager::Script_GetEntitiesWithInterface> ("GetEntitiesWithInterface");
m_ScriptInterface.RegisterFunction, int, CComponentManager::Script_GetComponentsWithInterface> ("GetComponentsWithInterface");
m_ScriptInterface.RegisterFunction ("PostMessage");
m_ScriptInterface.RegisterFunction ("BroadcastMessage");
m_ScriptInterface.RegisterFunction ("AddEntity");
m_ScriptInterface.RegisterFunction ("AddLocalEntity");
m_ScriptInterface.RegisterFunction ("DestroyEntity");
m_ScriptInterface.RegisterFunction ("FlushDestroyedEntities");
m_ScriptInterface.RegisterFunction ("DataFileExists");
- m_ScriptInterface.RegisterFunction, std::wstring, bool, CComponentManager::Script_FindJSONFiles> ("FindJSONFiles");
}
// Define MT_*, IID_* as script globals, and store their names
#define MESSAGE(name) m_ScriptInterface.SetGlobal("MT_" #name, (int)MT_##name);
#define INTERFACE(name) \
m_ScriptInterface.SetGlobal("IID_" #name, (int)IID_##name); \
m_InterfaceIdsByName[#name] = IID_##name;
#define COMPONENT(name)
#include "simulation2/TypeList.h"
#undef MESSAGE
#undef INTERFACE
#undef COMPONENT
m_ScriptInterface.SetGlobal("INVALID_ENTITY", (int)INVALID_ENTITY);
m_ScriptInterface.SetGlobal("SYSTEM_ENTITY", (int)SYSTEM_ENTITY);
m_ComponentsByInterface.resize(IID__LastNative);
ResetState();
}
CComponentManager::~CComponentManager()
{
ResetState();
}
void CComponentManager::LoadComponentTypes()
{
#define MESSAGE(name) \
RegisterMessageType(MT_##name, #name);
#define INTERFACE(name) \
extern void RegisterComponentInterface_##name(ScriptInterface&); \
RegisterComponentInterface_##name(m_ScriptInterface);
#define COMPONENT(name) \
extern void RegisterComponentType_##name(CComponentManager&); \
m_CurrentComponent = CID_##name; \
RegisterComponentType_##name(*this);
#include "simulation2/TypeList.h"
m_CurrentComponent = CID__Invalid;
#undef MESSAGE
#undef INTERFACE
#undef COMPONENT
}
bool CComponentManager::LoadScript(const VfsPath& filename, bool hotload)
{
m_CurrentlyHotloading = hotload;
CVFSFile file;
PSRETURN loadOk = file.Load(g_VFS, filename);
if (loadOk != PSRETURN_OK) // VFS will log the failed file and the reason
return false;
std::string content = file.DecodeUTF8(); // assume it's UTF-8
bool ok = m_ScriptInterface.LoadScript(filename, content);
return ok;
}
void CComponentManager::Script_RegisterComponentType_Common(ScriptInterface::CxPrivate* pCxPrivate, int iid, const std::string& cname, JS::HandleValue ctor, bool reRegister, bool systemComponent)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
JSContext* cx = componentManager->m_ScriptInterface.GetContext();
JSAutoRequest rq(cx);
// Find the C++ component that wraps the interface
int cidWrapper = componentManager->GetScriptWrapper(iid);
if (cidWrapper == CID__Invalid)
{
componentManager->m_ScriptInterface.ReportError("Invalid interface id");
return;
}
const ComponentType& ctWrapper = componentManager->m_ComponentTypesById[cidWrapper];
bool mustReloadComponents = false; // for hotloading
ComponentTypeId cid = componentManager->LookupCID(cname);
if (cid == CID__Invalid)
{
if (reRegister)
{
std::string msg("ReRegistering component type that was not registered before '"+cname+"'");
componentManager->m_ScriptInterface.ReportError(msg.c_str());
return;
}
// Allocate a new cid number
cid = componentManager->m_NextScriptComponentTypeId++;
componentManager->m_ComponentTypeIdsByName[cname] = cid;
if (systemComponent)
componentManager->MarkScriptedComponentForSystemEntity(cid);
}
else
{
// Component type is already loaded, so do hotloading:
if (!componentManager->m_CurrentlyHotloading && !reRegister)
{
std::string msg("Registering component type with already-registered name '"+cname+"'");
componentManager->m_ScriptInterface.ReportError(msg.c_str());
return;
}
const ComponentType& ctPrevious = componentManager->m_ComponentTypesById[cid];
// We can only replace scripted component types, not native ones
if (ctPrevious.type != CT_Script)
{
std::string msg("Loading script component type with same name '"+cname+"' as native component");
componentManager->m_ScriptInterface.ReportError(msg.c_str());
return;
}
// We don't support changing the IID of a component type (it would require fiddling
// around with m_ComponentsByInterface and being careful to guarantee uniqueness per entity)
if (ctPrevious.iid != iid)
{
// ...though it only matters if any components exist with this type
if (!componentManager->m_ComponentsByTypeId[cid].empty())
{
componentManager->m_ScriptInterface.ReportError("Hotloading script component type mustn't change interface ID");
return;
}
}
// Remove the old component type's message subscriptions
std::map >::iterator it;
for (it = componentManager->m_LocalMessageSubscriptions.begin(); it != componentManager->m_LocalMessageSubscriptions.end(); ++it)
{
std::vector& types = it->second;
std::vector::iterator ctit = find(types.begin(), types.end(), cid);
if (ctit != types.end())
types.erase(ctit);
}
for (it = componentManager->m_GlobalMessageSubscriptions.begin(); it != componentManager->m_GlobalMessageSubscriptions.end(); ++it)
{
std::vector& types = it->second;
std::vector::iterator ctit = find(types.begin(), types.end(), cid);
if (ctit != types.end())
types.erase(ctit);
}
mustReloadComponents = true;
}
std::string schema = "";
{
JS::RootedValue prototype(cx);
if (componentManager->m_ScriptInterface.GetProperty(ctor, "prototype", &prototype) &&
componentManager->m_ScriptInterface.HasProperty(prototype, "Schema"))
{
componentManager->m_ScriptInterface.GetProperty(prototype, "Schema", schema);
}
}
// Construct a new ComponentType, using the wrapper's alloc functions
ComponentType ct(
CT_Script,
iid,
ctWrapper.alloc,
ctWrapper.dealloc,
cname,
schema,
DefPersistentRooted(cx, ctor)
);
componentManager->m_ComponentTypesById[cid] = std::move(ct);
componentManager->m_CurrentComponent = cid; // needed by Subscribe
// Find all the ctor prototype's On* methods, and subscribe to the appropriate messages:
JS::RootedValue protoVal(cx);
if (!componentManager->m_ScriptInterface.GetProperty(ctor, "prototype", &protoVal))
return; // error
std::vector methods;
JS::RootedObject proto(cx);
if (!protoVal.isObjectOrNull())
return; // error
proto = protoVal.toObjectOrNull();
if (!componentManager->m_ScriptInterface.EnumeratePropertyNamesWithPrefix(protoVal, "On", methods))
return; // error
for (std::vector::const_iterator it = methods.begin(); it != methods.end(); ++it)
{
std::string name = (*it).substr(2); // strip the "On" prefix
// Handle "OnGlobalFoo" functions specially
bool isGlobal = false;
if (name.substr(0, 6) == "Global")
{
isGlobal = true;
name = name.substr(6);
}
std::map::const_iterator mit = componentManager->m_MessageTypeIdsByName.find(name);
if (mit == componentManager->m_MessageTypeIdsByName.end())
{
std::string msg("Registered component has unrecognised '" + *it + "' message handler method");
componentManager->m_ScriptInterface.ReportError(msg.c_str());
return;
}
if (isGlobal)
componentManager->SubscribeGloballyToMessageType(mit->second);
else
componentManager->SubscribeToMessageType(mit->second);
}
componentManager->m_CurrentComponent = CID__Invalid;
if (mustReloadComponents)
{
// For every script component with this cid, we need to switch its
// prototype from the old constructor's prototype property to the new one's
const std::map& comps = componentManager->m_ComponentsByTypeId[cid];
std::map::const_iterator eit = comps.begin();
for (; eit != comps.end(); ++eit)
{
JS::RootedValue instance(cx, eit->second->GetJSInstance());
if (!instance.isNull())
{
componentManager->m_ScriptInterface.SetPrototype(instance, protoVal);
}
}
}
}
void CComponentManager::Script_RegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, const std::string& cname, JS::HandleValue ctor)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
componentManager->Script_RegisterComponentType_Common(pCxPrivate, iid, cname, ctor, false, false);
componentManager->m_ScriptInterface.SetGlobal(cname.c_str(), ctor, componentManager->m_CurrentlyHotloading);
}
void CComponentManager::Script_RegisterSystemComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, const std::string& cname, JS::HandleValue ctor)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
componentManager->Script_RegisterComponentType_Common(pCxPrivate, iid, cname, ctor, false, true);
componentManager->m_ScriptInterface.SetGlobal(cname.c_str(), ctor, componentManager->m_CurrentlyHotloading);
}
void CComponentManager::Script_ReRegisterComponentType(ScriptInterface::CxPrivate* pCxPrivate, int iid, const std::string& cname, JS::HandleValue ctor)
{
Script_RegisterComponentType_Common(pCxPrivate, iid, cname, ctor, true, false);
}
void CComponentManager::Script_RegisterInterface(ScriptInterface::CxPrivate* pCxPrivate, const std::string& name)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
std::map::iterator it = componentManager->m_InterfaceIdsByName.find(name);
if (it != componentManager->m_InterfaceIdsByName.end())
{
// Redefinitions are fine (and just get ignored) when hotloading; otherwise
// they're probably unintentional and should be reported
if (!componentManager->m_CurrentlyHotloading)
{
std::string msg("Registering interface with already-registered name '"+name+"'");
componentManager->m_ScriptInterface.ReportError(msg.c_str());
}
return;
}
// IIDs start at 1, so size+1 is the next unused one
size_t id = componentManager->m_InterfaceIdsByName.size() + 1;
componentManager->m_InterfaceIdsByName[name] = (InterfaceId)id;
componentManager->m_ComponentsByInterface.resize(id+1); // add one so we can index by InterfaceId
componentManager->m_ScriptInterface.SetGlobal(("IID_" + name).c_str(), (int)id);
}
void CComponentManager::Script_RegisterMessageType(ScriptInterface::CxPrivate* pCxPrivate, const std::string& name)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
std::map::iterator it = componentManager->m_MessageTypeIdsByName.find(name);
if (it != componentManager->m_MessageTypeIdsByName.end())
{
// Redefinitions are fine (and just get ignored) when hotloading; otherwise
// they're probably unintentional and should be reported
if (!componentManager->m_CurrentlyHotloading)
{
std::string msg("Registering message type with already-registered name '"+name+"'");
componentManager->m_ScriptInterface.ReportError(msg.c_str());
}
return;
}
// MTIDs start at 1, so size+1 is the next unused one
size_t id = componentManager->m_MessageTypeIdsByName.size() + 1;
componentManager->RegisterMessageType((MessageTypeId)id, name.c_str());
componentManager->m_ScriptInterface.SetGlobal(("MT_" + name).c_str(), (int)id);
}
void CComponentManager::Script_RegisterGlobal(ScriptInterface::CxPrivate* pCxPrivate, const std::string& name, JS::HandleValue value)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
// Set the value, and accept duplicates only if hotloading (otherwise it's an error,
// in order to detect accidental duplicate definitions of globals)
componentManager->m_ScriptInterface.SetGlobal(name.c_str(), value, componentManager->m_CurrentlyHotloading);
}
IComponent* CComponentManager::Script_QueryInterface(ScriptInterface::CxPrivate* pCxPrivate, int ent, int iid)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
IComponent* component = componentManager->QueryInterface((entity_id_t)ent, iid);
return component;
}
std::vector CComponentManager::Script_GetEntitiesWithInterface(ScriptInterface::CxPrivate* pCxPrivate, int iid)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
std::vector ret;
const InterfaceListUnordered& ents = componentManager->GetEntitiesWithInterfaceUnordered(iid);
for (InterfaceListUnordered::const_iterator it = ents.begin(); it != ents.end(); ++it)
if (!ENTITY_IS_LOCAL(it->first))
ret.push_back(it->first);
std::sort(ret.begin(), ret.end());
return ret;
}
std::vector CComponentManager::Script_GetComponentsWithInterface(ScriptInterface::CxPrivate* pCxPrivate, int iid)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
std::vector ret;
InterfaceList ents = componentManager->GetEntitiesWithInterface(iid);
for (InterfaceList::const_iterator it = ents.begin(); it != ents.end(); ++it)
ret.push_back(it->second); // TODO: maybe we should exclude local entities
return ret;
}
CMessage* CComponentManager::ConstructMessage(int mtid, JS::HandleValue data)
{
if (mtid == MT__Invalid || mtid > (int)m_MessageTypeIdsByName.size()) // (IDs start at 1 so use '>' here)
LOGERROR("PostMessage with invalid message type ID '%d'", mtid);
if (mtid < MT__LastNative)
{
return CMessageFromJSVal(mtid, m_ScriptInterface, data);
}
else
{
return new CMessageScripted(m_ScriptInterface, mtid, m_MessageTypeNamesById[mtid], data);
}
}
void CComponentManager::Script_PostMessage(ScriptInterface::CxPrivate* pCxPrivate, int ent, int mtid, JS::HandleValue data)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
CMessage* msg = componentManager->ConstructMessage(mtid, data);
if (!msg)
return; // error
componentManager->PostMessage(ent, *msg);
delete msg;
}
void CComponentManager::Script_BroadcastMessage(ScriptInterface::CxPrivate* pCxPrivate, int mtid, JS::HandleValue data)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
CMessage* msg = componentManager->ConstructMessage(mtid, data);
if (!msg)
return; // error
componentManager->BroadcastMessage(*msg);
delete msg;
}
int CComponentManager::Script_AddEntity(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
std::wstring name(templateName.begin(), templateName.end());
// TODO: should validate the string to make sure it doesn't contain scary characters
// that will let it access non-component-template files
entity_id_t ent = componentManager->AddEntity(name, componentManager->AllocateNewEntity());
return (int)ent;
}
int CComponentManager::Script_AddLocalEntity(ScriptInterface::CxPrivate* pCxPrivate, const std::string& templateName)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
std::wstring name(templateName.begin(), templateName.end());
// TODO: should validate the string to make sure it doesn't contain scary characters
// that will let it access non-component-template files
entity_id_t ent = componentManager->AddEntity(name, componentManager->AllocateNewLocalEntity());
return (int)ent;
}
void CComponentManager::Script_DestroyEntity(ScriptInterface::CxPrivate* pCxPrivate, int ent)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
componentManager->DestroyComponentsSoon(ent);
}
void CComponentManager::Script_FlushDestroyedEntities(ScriptInterface::CxPrivate *pCxPrivate)
{
CComponentManager* componentManager = static_cast (pCxPrivate->pCBData);
componentManager->FlushDestroyedComponents();
}
void CComponentManager::ResetState()
{
// Delete all dynamic message subscriptions
m_DynamicMessageSubscriptionsNonsync.clear();
m_DynamicMessageSubscriptionsNonsyncByComponent.clear();
// Delete all IComponents
std::map >::iterator iit = m_ComponentsByTypeId.begin();
for (; iit != m_ComponentsByTypeId.end(); ++iit)
{
std::map::iterator eit = iit->second.begin();
for (; eit != iit->second.end(); ++eit)
{
eit->second->Deinit();
m_ComponentTypesById[iit->first].dealloc(eit->second);
}
}
std::vector >::iterator ifcit = m_ComponentsByInterface.begin();
for (; ifcit != m_ComponentsByInterface.end(); ++ifcit)
ifcit->clear();
m_ComponentsByTypeId.clear();
// Delete all SEntityComponentCaches
std::unordered_map::iterator ccit = m_ComponentCaches.begin();
for (; ccit != m_ComponentCaches.end(); ++ccit)
free(ccit->second);
m_ComponentCaches.clear();
m_SystemEntity = CEntityHandle();
m_DestructionQueue.clear();
// Reset IDs
m_NextEntityId = SYSTEM_ENTITY + 1;
m_NextLocalEntityId = FIRST_LOCAL_ENTITY;
}
void CComponentManager::SetRNGSeed(u32 seed)
{
m_RNG.seed(seed);
}
void CComponentManager::RegisterComponentType(InterfaceId iid, ComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc,
const char* name, const std::string& schema)
{
ComponentType c(CT_Native, iid, alloc, dealloc, name, schema, DefPersistentRooted());
m_ComponentTypesById.insert(std::make_pair(cid, std::move(c)));
m_ComponentTypeIdsByName[name] = cid;
}
void CComponentManager::RegisterComponentTypeScriptWrapper(InterfaceId iid, ComponentTypeId cid, AllocFunc alloc,
DeallocFunc dealloc, const char* name, const std::string& schema)
{
ComponentType c(CT_ScriptWrapper, iid, alloc, dealloc, name, schema, DefPersistentRooted());
m_ComponentTypesById.insert(std::make_pair(cid, std::move(c)));
m_ComponentTypeIdsByName[name] = cid;
// TODO: merge with RegisterComponentType
}
void CComponentManager::MarkScriptedComponentForSystemEntity(CComponentManager::ComponentTypeId cid)
{
m_ScriptedSystemComponents.push_back(cid);
}
void CComponentManager::RegisterMessageType(MessageTypeId mtid, const char* name)
{
m_MessageTypeIdsByName[name] = mtid;
m_MessageTypeNamesById[mtid] = name;
}
void CComponentManager::SubscribeToMessageType(MessageTypeId mtid)
{
// TODO: verify mtid
ENSURE(m_CurrentComponent != CID__Invalid);
std::vector& types = m_LocalMessageSubscriptions[mtid];
types.push_back(m_CurrentComponent);
std::sort(types.begin(), types.end()); // TODO: just sort once at the end of LoadComponents
}
void CComponentManager::SubscribeGloballyToMessageType(MessageTypeId mtid)
{
// TODO: verify mtid
ENSURE(m_CurrentComponent != CID__Invalid);
std::vector& types = m_GlobalMessageSubscriptions[mtid];
types.push_back(m_CurrentComponent);
std::sort(types.begin(), types.end()); // TODO: just sort once at the end of LoadComponents
}
void CComponentManager::FlattenDynamicSubscriptions()
{
std::map::iterator it;
for (it = m_DynamicMessageSubscriptionsNonsync.begin();
it != m_DynamicMessageSubscriptionsNonsync.end(); ++it)
{
it->second.Flatten();
}
}
void CComponentManager::DynamicSubscriptionNonsync(MessageTypeId mtid, IComponent* component, bool enable)
{
if (enable)
{
bool newlyInserted = m_DynamicMessageSubscriptionsNonsyncByComponent[component].insert(mtid).second;
if (newlyInserted)
m_DynamicMessageSubscriptionsNonsync[mtid].Add(component);
}
else
{
size_t numRemoved = m_DynamicMessageSubscriptionsNonsyncByComponent[component].erase(mtid);
if (numRemoved)
m_DynamicMessageSubscriptionsNonsync[mtid].Remove(component);
}
}
void CComponentManager::RemoveComponentDynamicSubscriptions(IComponent* component)
{
std::map >::iterator it = m_DynamicMessageSubscriptionsNonsyncByComponent.find(component);
if (it == m_DynamicMessageSubscriptionsNonsyncByComponent.end())
return;
std::set::iterator mtit;
for (mtit = it->second.begin(); mtit != it->second.end(); ++mtit)
{
m_DynamicMessageSubscriptionsNonsync[*mtit].Remove(component);
// Need to flatten the subscription lists immediately to avoid dangling IComponent* references
m_DynamicMessageSubscriptionsNonsync[*mtit].Flatten();
}
m_DynamicMessageSubscriptionsNonsyncByComponent.erase(it);
}
CComponentManager::ComponentTypeId CComponentManager::LookupCID(const std::string& cname) const
{
std::map::const_iterator it = m_ComponentTypeIdsByName.find(cname);
if (it == m_ComponentTypeIdsByName.end())
return CID__Invalid;
return it->second;
}
std::string CComponentManager::LookupComponentTypeName(ComponentTypeId cid) const
{
std::map::const_iterator it = m_ComponentTypesById.find(cid);
if (it == m_ComponentTypesById.end())
return "";
return it->second.name;
}
CComponentManager::ComponentTypeId CComponentManager::GetScriptWrapper(InterfaceId iid)
{
if (iid >= IID__LastNative && iid <= (int)m_InterfaceIdsByName.size()) // use <= since IDs start at 1
return CID_UnknownScript;
std::map::const_iterator it = m_ComponentTypesById.begin();
for (; it != m_ComponentTypesById.end(); ++it)
if (it->second.iid == iid && it->second.type == CT_ScriptWrapper)
return it->first;
std::map::const_iterator iiit = m_InterfaceIdsByName.begin();
for (; iiit != m_InterfaceIdsByName.end(); ++iiit)
if (iiit->second == iid)
{
LOGERROR("No script wrapper found for interface id %d '%s'", iid, iiit->first.c_str());
return CID__Invalid;
}
LOGERROR("No script wrapper found for interface id %d", iid);
return CID__Invalid;
}
entity_id_t CComponentManager::AllocateNewEntity()
{
entity_id_t id = m_NextEntityId++;
// TODO: check for overflow
return id;
}
entity_id_t CComponentManager::AllocateNewLocalEntity()
{
entity_id_t id = m_NextLocalEntityId++;
// TODO: check for overflow
return id;
}
entity_id_t CComponentManager::AllocateNewEntity(entity_id_t preferredId)
{
// TODO: ensure this ID hasn't been allocated before
// (this might occur with broken map files)
// Trying to actually add two entities with the same id will fail in AddEntitiy
entity_id_t id = preferredId;
// Ensure this ID won't be allocated again
if (id >= m_NextEntityId)
m_NextEntityId = id+1;
// TODO: check for overflow
return id;
}
bool CComponentManager::AddComponent(CEntityHandle ent, ComponentTypeId cid, const CParamNode& paramNode)
{
IComponent* component = ConstructComponent(ent, cid);
if (!component)
return false;
component->Init(paramNode);
return true;
}
void CComponentManager::AddSystemComponents(bool skipScriptedComponents, bool skipAI)
{
CParamNode noParam;
AddComponent(m_SystemEntity, CID_TemplateManager, noParam);
AddComponent(m_SystemEntity, CID_CinemaManager, noParam);
AddComponent(m_SystemEntity, CID_CommandQueue, noParam);
AddComponent(m_SystemEntity, CID_ObstructionManager, noParam);
AddComponent(m_SystemEntity, CID_ParticleManager, noParam);
AddComponent(m_SystemEntity, CID_Pathfinder, noParam);
AddComponent(m_SystemEntity, CID_ProjectileManager, noParam);
AddComponent(m_SystemEntity, CID_RangeManager, noParam);
AddComponent(m_SystemEntity, CID_SoundManager, noParam);
AddComponent(m_SystemEntity, CID_Terrain, noParam);
AddComponent(m_SystemEntity, CID_TerritoryManager, noParam);
AddComponent(m_SystemEntity, CID_UnitRenderer, noParam);
AddComponent(m_SystemEntity, CID_WaterManager, noParam);
// Add scripted system components:
if (!skipScriptedComponents)
{
for (uint32_t i = 0; i < m_ScriptedSystemComponents.size(); ++i)
AddComponent(m_SystemEntity, m_ScriptedSystemComponents[i], noParam);
if (!skipAI)
AddComponent(m_SystemEntity, CID_AIManager, noParam);
}
}
IComponent* CComponentManager::ConstructComponent(CEntityHandle ent, ComponentTypeId cid)
{
JSContext* cx = m_ScriptInterface.GetContext();
JSAutoRequest rq(cx);
std::map::const_iterator it = m_ComponentTypesById.find(cid);
if (it == m_ComponentTypesById.end())
{
LOGERROR("Invalid component id %d", cid);
return NULL;
}
const ComponentType& ct = it->second;
ENSURE((size_t)ct.iid < m_ComponentsByInterface.size());
boost::unordered_map& emap1 = m_ComponentsByInterface[ct.iid];
if (emap1.find(ent.GetId()) != emap1.end())
{
LOGERROR("Multiple components for interface %d", ct.iid);
return NULL;
}
std::map& emap2 = m_ComponentsByTypeId[cid];
// If this is a scripted component, construct the appropriate JS object first
JS::RootedValue obj(cx);
if (ct.type == CT_Script)
{
m_ScriptInterface.CallConstructor(ct.ctor.get(), JS::HandleValueArray::empty(), &obj);
if (obj.isNull())
{
LOGERROR("Script component constructor failed");
return NULL;
}
}
// Construct the new component
IComponent* component = ct.alloc(m_ScriptInterface, obj);
ENSURE(component);
component->SetEntityHandle(ent);
component->SetSimContext(m_SimContext);
// Store a reference to the new component
emap1.insert(std::make_pair(ent.GetId(), component));
emap2.insert(std::make_pair(ent.GetId(), component));
// TODO: We need to more careful about this - if an entity is constructed by a component
// while we're iterating over all components, this will invalidate the iterators and everything
// will break.
// We probably need some kind of delayed addition, so they get pushed onto a queue and then
// inserted into the world later on. (Be careful about immediation deletion in that case, too.)
SEntityComponentCache* cache = ent.GetComponentCache();
ENSURE(cache != NULL && ct.iid < (int)cache->numInterfaces && cache->interfaces[ct.iid] == NULL);
cache->interfaces[ct.iid] = component;
return component;
}
void CComponentManager::AddMockComponent(CEntityHandle ent, InterfaceId iid, IComponent& component)
{
// Just add it into the by-interface map, not the by-component-type map,
// so it won't be considered for messages or deletion etc
boost::unordered_map& emap1 = m_ComponentsByInterface.at(iid);
if (emap1.find(ent.GetId()) != emap1.end())
debug_warn(L"Multiple components for interface");
emap1.insert(std::make_pair(ent.GetId(), &component));
SEntityComponentCache* cache = ent.GetComponentCache();
ENSURE(cache != NULL && iid < (int)cache->numInterfaces && cache->interfaces[iid] == NULL);
cache->interfaces[iid] = &component;
}
CEntityHandle CComponentManager::AllocateEntityHandle(entity_id_t ent)
{
// Interface IDs start at 1, and SEntityComponentCache is defined with a 1-sized array,
// so we need space for an extra m_InterfaceIdsByName.size() items
SEntityComponentCache* cache = (SEntityComponentCache*)calloc(1,
sizeof(SEntityComponentCache) + sizeof(IComponent*) * m_InterfaceIdsByName.size());
ENSURE(cache != NULL);
cache->numInterfaces = m_InterfaceIdsByName.size() + 1;
ENSURE(m_ComponentCaches.find(ent) == m_ComponentCaches.end());
m_ComponentCaches[ent] = cache;
return CEntityHandle(ent, cache);
}
CEntityHandle CComponentManager::LookupEntityHandle(entity_id_t ent, bool allowCreate)
{
std::unordered_map::iterator it;
it = m_ComponentCaches.find(ent);
if (it == m_ComponentCaches.end())
{
if (allowCreate)
return AllocateEntityHandle(ent);
else
return CEntityHandle(ent, NULL);
}
else
return CEntityHandle(ent, it->second);
}
void CComponentManager::InitSystemEntity()
{
ENSURE(m_SystemEntity.GetId() == INVALID_ENTITY);
m_SystemEntity = AllocateEntityHandle(SYSTEM_ENTITY);
m_SimContext.SetSystemEntity(m_SystemEntity);
}
entity_id_t CComponentManager::AddEntity(const std::wstring& templateName, entity_id_t ent)
{
ICmpTemplateManager *cmpTemplateManager = static_cast (QueryInterface(SYSTEM_ENTITY, IID_TemplateManager));
if (!cmpTemplateManager)
{
debug_warn(L"No ICmpTemplateManager loaded");
return INVALID_ENTITY;
}
const CParamNode* tmpl = cmpTemplateManager->LoadTemplate(ent, utf8_from_wstring(templateName));
if (!tmpl)
return INVALID_ENTITY; // LoadTemplate will have reported the error
// This also ensures that ent does not exist
CEntityHandle handle = AllocateEntityHandle(ent);
// Construct a component for each child of the root element
const CParamNode::ChildrenMap& tmplChilds = tmpl->GetChildren();
for (CParamNode::ChildrenMap::const_iterator it = tmplChilds.begin(); it != tmplChilds.end(); ++it)
{
// Ignore attributes on the root element
if (it->first.length() && it->first[0] == '@')
continue;
CComponentManager::ComponentTypeId cid = LookupCID(it->first);
if (cid == CID__Invalid)
{
LOGERROR("Unrecognised component type name '%s' in entity template '%s'", it->first, utf8_from_wstring(templateName));
return INVALID_ENTITY;
}
if (!AddComponent(handle, cid, it->second))
{
LOGERROR("Failed to construct component type name '%s' in entity template '%s'", it->first, utf8_from_wstring(templateName));
return INVALID_ENTITY;
}
// TODO: maybe we should delete already-constructed components if one of them fails?
}
CMessageCreate msg(ent);
PostMessage(ent, msg);
return ent;
}
void CComponentManager::DestroyComponentsSoon(entity_id_t ent)
{
m_DestructionQueue.push_back(ent);
}
void CComponentManager::FlushDestroyedComponents()
{
PROFILE2("Flush Destroyed Components");
while (!m_DestructionQueue.empty())
{
// Make a copy of the destruction queue, so that the iterators won't be invalidated if the
// CMessageDestroy handlers try to destroy more entities themselves
std::vector queue;
queue.swap(m_DestructionQueue);
for (std::vector::iterator it = queue.begin(); it != queue.end(); ++it)
{
entity_id_t ent = *it;
CEntityHandle handle = LookupEntityHandle(ent);
CMessageDestroy msg(ent);
PostMessage(ent, msg);
// Flatten all the dynamic subscriptions to ensure there are no dangling
// references in the 'removed' lists to components we're going to delete
// Some components may have dynamically unsubscribed following the Destroy message
FlattenDynamicSubscriptions();
// Destroy the components, and remove from m_ComponentsByTypeId:
std::map >::iterator iit = m_ComponentsByTypeId.begin();
for (; iit != m_ComponentsByTypeId.end(); ++iit)
{
std::map::iterator eit = iit->second.find(ent);
if (eit != iit->second.end())
{
eit->second->Deinit();
RemoveComponentDynamicSubscriptions(eit->second);
m_ComponentTypesById[iit->first].dealloc(eit->second);
iit->second.erase(ent);
handle.GetComponentCache()->interfaces[m_ComponentTypesById[iit->first].iid] = NULL;
}
}
free(handle.GetComponentCache());
m_ComponentCaches.erase(ent);
// Remove from m_ComponentsByInterface
std::vector >::iterator ifcit = m_ComponentsByInterface.begin();
for (; ifcit != m_ComponentsByInterface.end(); ++ifcit)
{
ifcit->erase(ent);
}
}
}
}
IComponent* CComponentManager::QueryInterface(entity_id_t ent, InterfaceId iid) const
{
if ((size_t)iid >= m_ComponentsByInterface.size())
{
// Invalid iid
return NULL;
}
boost::unordered_map::const_iterator eit = m_ComponentsByInterface[iid].find(ent);
if (eit == m_ComponentsByInterface[iid].end())
{
// This entity doesn't implement this interface
return NULL;
}
return eit->second;
}
CComponentManager::InterfaceList CComponentManager::GetEntitiesWithInterface(InterfaceId iid) const
{
std::vector > ret;
if ((size_t)iid >= m_ComponentsByInterface.size())
{
// Invalid iid
return ret;
}
ret.reserve(m_ComponentsByInterface[iid].size());
boost::unordered_map::const_iterator it = m_ComponentsByInterface[iid].begin();
for (; it != m_ComponentsByInterface[iid].end(); ++it)
ret.push_back(*it);
std::sort(ret.begin(), ret.end()); // lexicographic pair comparison means this'll sort by entity ID
return ret;
}
static CComponentManager::InterfaceListUnordered g_EmptyEntityMap;
const CComponentManager::InterfaceListUnordered& CComponentManager::GetEntitiesWithInterfaceUnordered(InterfaceId iid) const
{
if ((size_t)iid >= m_ComponentsByInterface.size())
{
// Invalid iid
return g_EmptyEntityMap;
}
return m_ComponentsByInterface[iid];
}
void CComponentManager::PostMessage(entity_id_t ent, const CMessage& msg)
{
PROFILE2_IFSPIKE("Post Message", 0.0005);
PROFILE2_ATTR("%s", msg.GetScriptHandlerName());
// Send the message to components of ent, that subscribed locally to this message
std::map >::const_iterator it;
it = m_LocalMessageSubscriptions.find(msg.GetType());
if (it != m_LocalMessageSubscriptions.end())
{
std::vector::const_iterator ctit = it->second.begin();
for (; ctit != it->second.end(); ++ctit)
{
// Find the component instances of this type (if any)
std::map >::const_iterator emap = m_ComponentsByTypeId.find(*ctit);
if (emap == m_ComponentsByTypeId.end())
continue;
// Send the message to all of them
std::map::const_iterator eit = emap->second.find(ent);
if (eit != emap->second.end())
eit->second->HandleMessage(msg, false);
}
}
SendGlobalMessage(ent, msg);
}
void CComponentManager::BroadcastMessage(const CMessage& msg)
{
// Send the message to components of all entities that subscribed locally to this message
std::map >::const_iterator it;
it = m_LocalMessageSubscriptions.find(msg.GetType());
if (it != m_LocalMessageSubscriptions.end())
{
std::vector::const_iterator ctit = it->second.begin();
for (; ctit != it->second.end(); ++ctit)
{
// Find the component instances of this type (if any)
std::map >::const_iterator emap = m_ComponentsByTypeId.find(*ctit);
if (emap == m_ComponentsByTypeId.end())
continue;
// Send the message to all of them
std::map::const_iterator eit = emap->second.begin();
for (; eit != emap->second.end(); ++eit)
eit->second->HandleMessage(msg, false);
}
}
SendGlobalMessage(INVALID_ENTITY, msg);
}
void CComponentManager::SendGlobalMessage(entity_id_t ent, const CMessage& msg)
{
PROFILE2_IFSPIKE("SendGlobalMessage", 0.001);
PROFILE2_ATTR("%s", msg.GetScriptHandlerName());
// (Common functionality for PostMessage and BroadcastMessage)
// Send the message to components of all entities that subscribed globally to this message
std::map >::const_iterator it;
it = m_GlobalMessageSubscriptions.find(msg.GetType());
if (it != m_GlobalMessageSubscriptions.end())
{
std::vector::const_iterator ctit = it->second.begin();
for (; ctit != it->second.end(); ++ctit)
{
// Special case: Messages for local entities shouldn't be sent to script
// components that subscribed globally, so that we don't have to worry about
// them accidentally picking up non-network-synchronised data.
if (ENTITY_IS_LOCAL(ent))
{
std::map::const_iterator it = m_ComponentTypesById.find(*ctit);
if (it != m_ComponentTypesById.end() && it->second.type == CT_Script)
continue;
}
// Find the component instances of this type (if any)
std::map >::const_iterator emap = m_ComponentsByTypeId.find(*ctit);
if (emap == m_ComponentsByTypeId.end())
continue;
// Send the message to all of them
std::map::const_iterator eit = emap->second.begin();
for (; eit != emap->second.end(); ++eit)
eit->second->HandleMessage(msg, true);
}
}
// Send the message to component instances that dynamically subscribed to this message
std::map::iterator dit = m_DynamicMessageSubscriptionsNonsync.find(msg.GetType());
if (dit != m_DynamicMessageSubscriptionsNonsync.end())
{
dit->second.Flatten();
const std::vector& dynamic = dit->second.GetComponents();
for (size_t i = 0; i < dynamic.size(); i++)
dynamic[i]->HandleMessage(msg, false);
}
}
std::string CComponentManager::GenerateSchema() const
{
std::string numericOperation =
""
""
""
"add"
"mul"
""
""
"";
std::string schema =
""
""
""
+ numericOperation +
""
""
"0"
+ numericOperation +
""
""
"0"
+ numericOperation +
""
""
""
""
""
""
""
""
""
""
""
""
"";
std::map > interfaceComponentTypes;
std::vector componentTypes;
for (std::map::const_iterator it = m_ComponentTypesById.begin(); it != m_ComponentTypesById.end(); ++it)
{
schema +=
""
""
"" + it->second.schema + ""
""
"";
interfaceComponentTypes[it->second.iid].push_back(it->second.name);
componentTypes.push_back(it->second.name);
}
// Declare the implementation of each interface, for documentation
for (std::map::const_iterator it = m_InterfaceIdsByName.begin(); it != m_InterfaceIdsByName.end(); ++it)
{
schema += "";
std::vector& cts = interfaceComponentTypes[it->second];
for (size_t i = 0; i < cts.size(); ++i)
schema += "";
schema += "";
}
// List all the component types, in alphabetical order (to match the reordering performed by CParamNode).
// (We do it this way, rather than ing all the interface definitions (which would additionally perform
// a check that we don't use multiple component types of the same interface in one file), because libxml2 gives
// useless error messages in the latter case; this way lets it report the real error.)
std::sort(componentTypes.begin(), componentTypes.end());
schema +=
""
""
"";
for (std::vector::const_iterator it = componentTypes.begin(); it != componentTypes.end(); ++it)
schema += "";
schema +=
""
"";
schema += "";
return schema;
}
bool CComponentManager::Script_DataFileExists(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& fileName)
{
VfsPath path = VfsPath(L"simulation/data") / fileName;
return VfsFileExists(path);
}
-
-Status CComponentManager::FindJSONFilesCallback(const VfsPath& pathname, const CFileInfo& UNUSED(fileInfo), const uintptr_t cbData)
-{
- FindJSONFilesCallbackData* data = (FindJSONFilesCallbackData*)cbData;
-
- VfsPath pathstem = pathname.ChangeExtension(L"");
- // Strip the root from the path
- std::wstring name = pathstem.string().substr(data->path.string().length());
-
- data->templates.push_back(std::string(name.begin(), name.end()));
-
- return INFO::OK;
-}
-
-std::vector CComponentManager::Script_FindJSONFiles(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& subPath, bool recursive)
-{
- FindJSONFilesCallbackData cbData;
- cbData.path = VfsPath(L"simulation/data/" + subPath + L"/");
-
- int dir_flags = 0;
- if (recursive) {
- dir_flags = vfs::DIR_RECURSIVE;
- }
-
- // Find all simulation/data/{subPath}/*.json recursively
- Status ret = vfs::ForEachFile(g_VFS, cbData.path, FindJSONFilesCallback, (uintptr_t)&cbData, L"*.json", dir_flags);
- if (ret != INFO::OK)
- {
- // Some error reading directory
- wchar_t error[200];
- LOGERROR("Error reading directory '%s': %s", cbData.path.string8(), utf8_from_wstring(StatusDescription(ret, error, ARRAY_SIZE(error))));
- }
-
- return cbData.templates;
-}
Index: ps/trunk/source/simulation2/system/ComponentManager.h
===================================================================
--- ps/trunk/source/simulation2/system/ComponentManager.h (revision 20585)
+++ ps/trunk/source/simulation2/system/ComponentManager.h (revision 20586)
@@ -1,390 +1,380 @@
/* Copyright (C) 2017 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see .
*/
#ifndef INCLUDED_COMPONENTMANAGER
#define INCLUDED_COMPONENTMANAGER
#include "Entity.h"
#include "Components.h"
#include "scriptinterface/ScriptInterface.h"
#include "scriptinterface/ScriptVal.h"
#include "simulation2/helpers/Player.h"
#include "ps/Filesystem.h"
#include
#include
#include