Replace or remove uses of Engine.Console_Write. Patch by Adrian Fatol. Fixes #2141.

Cleans up list utility functions a bit.
Removes Engine.Console_Write.

This was SVN commit r15556.
This commit is contained in:
leper 2014-07-24 21:27:24 +00:00
parent 4278e93b97
commit 7c82e59ce5
6 changed files with 68 additions and 209 deletions

View File

@ -11,7 +11,10 @@
function removeItem(objectName, pos)
{
if (Engine.GetGUIObjectByName(objectName) == null)
Engine.Console_Write (sprintf(translate("%(functionName)s: %(object)s not found."), { functionName: "removeItem()", object: objectName }));
{
warn("removeItem(): " + objectName + " not found.");
return;
}
var list = Engine.GetGUIObjectByName(objectName).list;
var selected = Engine.GetGUIObjectByName(objectName).selected;
@ -25,15 +28,10 @@ function removeItem (objectName, pos)
// Update the selected so the same element remains selected.
if (selected == pos)
{
Engine.GetGUIObjectByName(objectName).selected = -1;
}
else
if (selected > pos)
{
else if (selected > pos)
Engine.GetGUIObjectByName(objectName).selected = selected - 1;
}
}
// ====================================================================
@ -41,7 +39,10 @@ function removeItem (objectName, pos)
function addItem(objectName, pos, value)
{
if (Engine.GetGUIObjectByName(objectName) == null)
Engine.Console_Write (sprintf(translate("%(functionName)s: %(object)s not found."), { functionName: "addItem()", object: objectName }));
{
warn("addItem(): " + objectName + " not found.");
return;
}
var list = Engine.GetGUIObjectByName(objectName).list;
var selected = Engine.GetGUIObjectByName(objectName).selected;
@ -55,10 +56,8 @@ function addItem (objectName, pos, value)
// Update the selected so the same element remains selected.
if (selected >= pos)
{
Engine.GetGUIObjectByName(objectName).selected = selected + 1;
}
}
// ====================================================================
@ -66,7 +65,10 @@ function addItem (objectName, pos, value)
function pushItem(objectName, value)
{
if (Engine.GetGUIObjectByName(objectName) == null)
Engine.Console_Write (sprintf(translate("%(functionName)s: %(object)s not found."), { functionName: "pushItem()", object: objectName }));
{
warn("pushItem(): " + objectName + " not found.");
return;
}
var list = Engine.GetGUIObjectByName(objectName).list;
list.push(value);
@ -81,16 +83,17 @@ function pushItem (objectName, value)
function popItem(objectName)
{
if (Engine.GetGUIObjectByName(objectName) == null)
Engine.Console_Write (sprintf(translate("%(functionName)s: %(object)s not found."), { functionName: "popItem()", object: objectName }));
{
warn("popItem(): " + objectName + " not found.");
return;
}
var selected = Engine.GetGUIObjectByName(objectName).selected;
removeItem(objectName, getNumItems(objectName)-1);
if (selected == getNumItems(objectName)-1)
{
Engine.GetGUIObjectByName(objectName).selected = -1;
}
}
// ====================================================================
@ -98,7 +101,10 @@ function popItem (objectName)
function getNumItems(objectName)
{
if (Engine.GetGUIObjectByName(objectName) == null)
Engine.Console_Write (sprintf(translate("%(functionName)s: %(object)s not found."), { functionName: "getNumItems()", object: objectName }));
{
warn("getNumItems(): " + objectName + " not found.");
return 0;
}
var list = Engine.GetGUIObjectByName(objectName).list;
return list.length;
@ -110,7 +116,10 @@ function getNumItems (objectName)
function getItemValue(objectName, pos)
{
if (Engine.GetGUIObjectByName(objectName) == null)
Engine.Console_Write (sprintf(translate("%(functionName)s: %(object)s not found."), { functionName: "getItemValue()", object: objectName }));
{
warn("getItemValue(): " + objectName + " not found.");
return "";
}
var list = Engine.GetGUIObjectByName(objectName).list;
return list[pos];
@ -122,7 +131,10 @@ function getItemValue (objectName, pos)
function getCurrItemValue(objectName)
{
if (Engine.GetGUIObjectByName(objectName) == null)
Engine.Console_Write (sprintf(translate("%(functionName)s: %(object)s not found."), { functionName: "getCurrItemValue()", object: objectName }));
{
warn("getCurrItemValue(): " + objectName + " not found.");
return "";
}
if (Engine.GetGUIObjectByName(objectName).selected == -1)
return "";
@ -138,7 +150,7 @@ function setCurrItemValue (objectName, string)
{
if (Engine.GetGUIObjectByName(objectName) == null)
{
Engine.Console_Write (sprintf(translate("%(functionName)s: %(object)s not found."), { functionName: "setCurrItemValue()", object: objectName }));
warn("setCurrItemValue(): " + objectName + " not found.");
return -1;
}
@ -158,7 +170,7 @@ function setCurrItemValue (objectName, string)
}
// Return -2 if failed to find value in list.
Engine.Console_Write (sprintf(translate("Requested string '%(string)s' not found in %(object)s's list."), { string: string, object: objectName }));
warn("Requested string '" + string + "' not found in " + objectName + "'s list.");
return -2;
}

View File

@ -4,85 +4,6 @@
*/
// ====================================================================
// Quick run-down of the basic audio commands:
// Save the specified audio file to handle "s".
// s = new Sound( "audio/music/menu_track.ogg" );
// Play the sound stored at handle "s" one time (it'll be automatically freed when the playback ends):
// s.play();
// Play the sound stored at handle "s" continuously:
// s.loop();
// Close "s" and free it from memory (use in conjunction with loop()):
// s.free();
// Adjust the gain (volume) of a sound (floating point range between 0 (silent) and 1 (max volume)).
// s.setGain(value);
// ====================================================================
function newRandomSound(soundType, soundSubType, soundPrePath)
{
// Return a random audio file by category, to be assigned to a handle.
var randomSoundPath;
switch (soundType)
{
case "music":
randomSoundPath = "audio/music/"
break;
case "ambient":
randomSoundPath = "audio/ambient/" + soundPrePath + "/";
break;
case "effect":
randomSoundPath = soundPrePath + "/";
break;
default:
break;
}
// Get names of sounds (attack, command, select, hit, pain).
// or
// Get names of "peace", "menu" (theme) and "battle" tracks.
var soundArray = Engine.BuildDirEntList(randomSoundPath, "*" + soundSubType + "*", false);
if (soundArray.length == 0)
{
Engine.Console_Write (sprintf("Failed to find sounds matching '*%(subtype)s*'", { soundSubType: subtype }));
return undefined;
}
// Get a random number within the sound's range.
var randomSound = getRandom (0, soundArray.length-1);
// Set name of track.
var randomFileName = soundArray[randomSound];
// Build path to random audio file.
randomSoundPath = randomFileName;
//Engine.Console_Write("Playing " + randomSoundPath + " ...");
switch (soundType)
{
case "music":
return new MusicSound(randomSoundPath);
break;
case "ambient":
return new AmbientSound(randomSoundPath);
break;
case "effect":
Engine.Console_Write(sprintf("am loading effect '*%(path)s*'", { path: randomSoundPath }));
break;
default:
break;
}
return new Sound(randomSoundPath);
}
// ====================================================================
function fadeOut (soundHandle, fadeDuration)
@ -123,49 +44,3 @@ function crossFade (outHandle, inHandle, fadeDuration)
return true;
}
// ====================================================================
//const AMBIENT_SOUND = "audio/ambient/dayscape/day_temperate_gen_03.ogg";
//const AMBIENT_TEMPERATE = "temperate";
//var currentAmbient;
//function playRandomAmbient(type)
//{
// switch (type)
// {
// case AMBIENT_TEMPERATE:
// // Seem to need the underscore at the end of "temperate" to avoid crash
// // (Might be caused by trying to randomly load day_temperate.xml)
// currentAmbient = newRandomSound("ambient", "temperate_", "dayscape");
// if (currentAmbient)
// {
// currentAmbient.loop();
// currentAmbient.setGain(0.8);
// }
// break;
//
// default:
// Engine.Console_Write("Unrecognized ambient type: " + type);
// break;
// }
//}
//
//function stopAmbient()
//{
// if (currentAmbient)
// {
// currentAmbient.fade(-1, 0.0, 5.0);
// currentAmbient = null;
// }
//}
//const BUTTON_SOUND = "audio/interface/ui/ui_button_longclick.ogg";
//function playButtonSound()
//{
// var buttonSound = new Sound(BUTTON_SOUND);
// buttonSound.play();
//}

View File

@ -19,7 +19,7 @@ function getCheatsData()
{
var currentCheat = parseJSONData("simulation/data/cheats/"+fileName+".json");
if (Object.keys(cheats).indexOf(currentCheat.Name) !== -1)
warn(sprintf("Cheat name '%(name)s' is already present", { name: currentCheat.Name }));
warn("Cheat name '" + currentCheat.Name + "' is already present");
else
cheats[currentCheat.Name] = currentCheat.Data;
}
@ -225,7 +225,7 @@ function handleNetMessage(message)
obj.hidden = false;
break;
default:
error(sprintf("Unrecognised netstatus type %(netType)s", { netType: message.status }));
error("Unrecognised netstatus type '" + message.status + "'");
break;
}
break;
@ -281,7 +281,7 @@ function handleNetMessage(message)
break;
default:
error(sprintf("Unrecognised net message type %(messageType)s", { messageType: message.type }));
error("Unrecognised net message type '" + message.type + "'");
}
}
@ -497,11 +497,6 @@ function addChatMessage(msg, playerAssignments)
{
if (msg.context !== "")
{
Engine.Console_Write(sprintf(translate("(%(context)s) * %(user)s %(message)s"), {
context: msg.context,
user: username,
message: message
}));
formatted = sprintf(translate("(%(context)s) * %(user)s %(message)s"), {
context: msg.context,
user: "[color=\"" + playerColor + "\"]" + username + "[/color]",
@ -510,10 +505,6 @@ function addChatMessage(msg, playerAssignments)
}
else
{
Engine.Console_Write(sprintf(translate("* %(user)s %(message)s"), {
user: username,
message: message
}));
formatted = sprintf(translate("* %(user)s %(message)s"), {
user: "[color=\"" + playerColor + "\"]" + username + "[/color]",
message: message
@ -526,11 +517,6 @@ function addChatMessage(msg, playerAssignments)
var formattedUserTag = sprintf(translate("<%(user)s>"), { user: "[color=\"" + playerColor + "\"]" + username + "[/color]" })
if (msg.context !== "")
{
Engine.Console_Write(sprintf(translate("(%(context)s) %(userTag)s %(message)s"), {
context: msg.context,
userTag: userTag,
message: message
}));
formatted = sprintf(translate("(%(context)s) %(userTag)s %(message)s"), {
context: msg.context,
userTag: formattedUserTag,
@ -539,7 +525,6 @@ function addChatMessage(msg, playerAssignments)
}
else
{
Engine.Console_Write(sprintf(translate("%(userTag)s %(message)s"), { userTag: userTag, message: message}));
formatted = sprintf(translate("%(userTag)s %(message)s"), { userTag: formattedUserTag, message: message});
}
}

View File

@ -876,16 +876,12 @@ function playRandomAmbient(type)
switch (type)
{
case AMBIENT_TEMPERATE:
// Seem to need the underscore at the end of "temperate" to avoid crash
// (Might be caused by trying to randomly load day_temperate.xml)
// currentAmbient = newRandomSound("ambient", "temperate_", "dayscape");
const AMBIENT = "audio/ambient/dayscape/day_temperate_gen_03.ogg";
Engine.PlayAmbientSound(AMBIENT, true);
break;
default:
Engine.Console_Write(sprintf(translate("Unrecognized ambient type: %(ambientType)s"), { ambientType: type }));
error("Unrecognized ambient type: '" + type + "'");
break;
}
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2013 Wildfire Games.
/* Copyright (C) 2014 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -46,16 +46,8 @@ void JSI_Console::SetVisibleEnabled(ScriptInterface::CxPrivate* UNUSED(pCxPrivat
g_Console->SetVisible(Enabled);
}
void JSI_Console::Write(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::wstring output)
{
if (!CheckGlobalInitialized())
return;
g_Console->InsertMessageRaw(output);
}
void JSI_Console::RegisterScriptFunctions(ScriptInterface& scriptInterface)
{
scriptInterface.RegisterFunction<bool, &JSI_Console::GetVisibleEnabled>("Console_GetVisibleEnabled");
scriptInterface.RegisterFunction<void, bool, &JSI_Console::SetVisibleEnabled>("Console_SetVisibleEnabled");
scriptInterface.RegisterFunction<void, std::wstring, &JSI_Console::Write>("Console_Write");
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2013 Wildfire Games.
/* Copyright (C) 2014 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -25,7 +25,6 @@ namespace JSI_Console
bool CheckGlobalInitialized();
bool GetVisibleEnabled(ScriptInterface::CxPrivate* pCxPrivate);
void SetVisibleEnabled(ScriptInterface::CxPrivate* pCxPrivate, bool Enabled);
void Write(ScriptInterface::CxPrivate* pCxPrivate, std::wstring output);
void RegisterScriptFunctions(ScriptInterface& scriptInterface);
}