Moderator
M
Moderator
08:25, 26th Jul 2013
Magtheridon96: Approved.
Magtheridon96: Approved.
//*************************************************************************************
//*
//* Macro by Almia
//*
//* Allows users to generate codes or simply generate CnP codes and replaces words
//* with wanted words for much easier replacement. JASS and GUI doesn't have this
//* kind of functionality, so I gave them the ability to do so. The system is similar
//* to vJASS Text Macro though has the following "ugly" features:
//* - Macro generates the code outside the map,read the next statement.
//* - Codes are generated inside a .bat file and you need to download the
//* generated codes from the .bat file.
//* - Copy paste generate codes into your map code.
//* - Because of these features, it uses Preload.
//* - You can't replace the macro variable names,
//* all are sequenced with an integer, so you need to use those integers
//* instead of these.
//*
//*************************************************************************************
//*
//* API
//*
//* function Macro takes nothing returns integer
//* Creates a macro instance
//*
//* function MacroAppend takes integer macro, string phrase returns nothing
//* Declares a macro phrase
//*
//* function MacroPushArgument takes integer macro, string val returns nothing
//* Declares a macro variable's value
//*
//* function MacroParse takes integer macro returns nothing
//* Run a macro and generates its code
//*
//* function DownloadMacro takes integer macro returns nothing
//* Download generated macro code
//*
//* function ClearMacro takes integer macro returns nothing
//* Clears macro memory
//*
//* function DestroyMacro takes integer macro returns nothing
//* Destroys a macro instance
//*
//*************************************************************************************
//*
//* How does Macro works?
//*
//* - First,you need to create a Macro Instance:
//* local integer macro = Macro()
//* - Do declare phrase use:
//* call MacroAppend(whichMacro, "someStatement")
//* - Variables are done like these:
//* {1}
//* {2}
//* - Declare a phrase with macro variable
//* call MacroAppend(whichMacro, "someSortOfStuffLike{1} or {2}")
//* - Those won't be complete without declaring what is the value of the macro variable
//* call MacroPushArgument(whichMacro, "This") // replaces {1}
//* call MacroPushArgument(whichMacro, "That") // replaces {2}
//* - Generate code by running:
//* call MacroParse(m)
//* - Download code:
//* call DownloadMacro(m)
//*
//*************************************************************************************
//*
//* About Macro Var
//*
//* Noticed that Macro variables uses integers. Setting macro variable integer's value
//* must be equal to how many times MacroVarSet was called to a given macro.
//* For example:
//* call MacroPushArgument(whichMacro, "that")
//* call MacroPushArgument(whichMacro, "this")
//* call MacroPushArgument(whichMacro, "those")
//*
//* "that" replaces {1}, "this" replaces {2}, "those" replaces {3}
//* Another example:
//* call MacroPushArgument(whichMacro, "hahah")
//* call BJDebugMsg("hahah")
//* call MacroPushArgument(whichMacro, "hahaha")
//*
//* "hahah" replaces {1} and "hahaha" replaces {2}
//*
//*************************************************************************************
constant function MACRO_FOLDER takes nothing returns string
return "MacroFiles\\"
endfunction
constant function MAP_NAME takes nothing returns string
return "testMap"
endfunction
function PreloadMacroPhrase takes integer macro, integer key returns string
local string s = LoadStr(udg_MacroTable, macro, key)
local integer i = 0
local integer i2 = StringLength(s)
local string s2
local string s3 = ""
local boolean open = false
loop
exitwhen i == i2
set s2 = SubString(s, i, i + 1)
if s2 == "{" then
set open = true
elseif s2 == "}" then
set open = false
elseif open then
set s3 = s3 + s2
elseif s3 != "" then
set s = SubString(s, 0, i - StringLength(s3) - 2) + LoadStr(udg_MacroTable, macro, 8192 + S2I(s3)) + SubString(s, i, i2)
set i2 = StringLength(s)
set s3 = ""
endif
set i = i + 1
endloop
return s
endfunction
function Macro takes nothing returns integer
local integer macro = udg_Macro_R[0]
if 0 == macro then
set macro = udg_Macro + 1
set udg_Macro = macro
else
set udg_Macro_R[0] = udg_Macro_R[macro]
endif
if null == udg_MacroTable then
set udg_MacroTable = InitHashtable()
call BJDebugMsg("Note:")
call BJDebugMsg("All macro files can be found in your Warcraft III folder -> " + MACRO_FOLDER() + MAP_NAME())
endif
set udg_Macro_Var[macro] = 0
set udg_Macro_Phrase[macro] = 0
set udg_Macro_Use[macro] = 0
call FlushChildHashtable(udg_MacroTable, macro)
return macro
endfunction
function MacroAppend takes integer macro, string phrase returns nothing
call SaveStr(udg_MacroTable, macro, udg_Macro_Phrase[macro], phrase)
set udg_Macro_Phrase[macro] = udg_Macro_Phrase[macro] + 1
endfunction
function MacroPushArgument takes integer macro, string val returns nothing
set udg_Macro_Var[macro] = udg_Macro_Var[macro] + 1
call SaveStr(udg_MacroTable, macro, 8192 + udg_Macro_Var[macro], val)
endfunction
function MacroParse takes integer macro returns nothing
local integer i = 0
loop
exitwhen i == udg_Macro_Phrase[macro]
call SaveStr(udg_MacroTable, macro, 16384 + (udg_Macro_Use[macro] * udg_Macro_Phrase[macro]) + i, "\")\r\n\techo " + PreloadMacroPhrase(macro, i) + " >> macro" + I2S(macro) + ".txt\r\n\t(\"")
set i = i + 1
endloop
set udg_Macro_Use[macro] = udg_Macro_Use[macro] + 1
set udg_Macro_Var[macro] = 0
endfunction
function DownloadMacro takes integer macro returns nothing
local integer i = 0
call PreloadGenClear()
call PreloadGenStart()
call Preload("\")\r\n\tdel macro" + I2S(macro) + ".txt\r\n\t(\"")
loop
exitwhen i == udg_Macro_Use[macro] * udg_Macro_Phrase[macro]
call Preload(LoadStr(udg_MacroTable, macro, 16384 + i))
set i = i + 1
endloop
call Preload("\")\r\n\tstart macro" + I2S(macro) + ".txt\r\n\t(\"")
call Preload("\")\r\n\tdel macroDL" + I2S(macro) + ".bat\r\n\t(\"")
call PreloadGenEnd(MACRO_FOLDER() + MAP_NAME() + "\\macroDL" + I2S(macro) + ".bat")
set udg_Macro_Use[macro] = 0
endfunction
function ClearMacro takes integer macro returns nothing
set udg_Macro_Var[macro] = 0
set udg_Macro_Preload[macro] = 0
set udg_Macro_Phrase[macro] = 0
call FlushChildHashtable(udg_MacroTable, macro)
endfunction
function DestroyMacro takes integer macro returns nothing
call ClearMacro(macro)
set udg_Macro_R[macro] = udg_Macro_R[0]
set udg_Macro_R[0] = macro
endfunction
function Test takes nothing returns nothing
local integer m = Macro()
// Declare macro phrases
call MacroAppend(m, "function Save{1} takes integer t, {2} a returns nothing")
call MacroAppend(m, " call Save{1}Handle(ht, t, 0, a)")
call MacroAppend(m, "endfunction")
//Declare macro variables
call MacroPushArgument(m, "Unit") // replaces {1}
call MacroPushArgument(m, "unit") // replaces {2}
//Generate macro code
call MacroParse(m)
//Declare macro variables
call MacroPushArgument(m, "Destructable") // replaces {1}
call MacroPushArgument(m, "destructable") // replaces {2}
//Generate macro code
call MacroParse(m)
//Download macro code
call DownloadMacro(m)
//Clear macro memory
call ClearMacro(m)
//Destroy macro
call DestroyMacro(m)
endfunction
//===========================================================================
function InitTrig_Temp takes nothing returns nothing
call Test()
endfunction