GenshinDataConverter/FileCodeOutput.jl
hypergraphUniverse 2e6010b2fb Update the code in 0.0.1alpha
for basic usage in C and Julia with 2 files
2022-03-07 11:07:16 +01:00

132 lines
3.8 KiB
Julia

# This is the Module for Code output in different style
# Specified for Genshin Impact, Types are Float32
module FileCodeOutput
using Dates
export Print2DMatrix,Print3DMatrix,PrintScriptVersion
# syntax dictionary
dictLanguage=Dict(
"C"=>Dict(
"termsuffix"=> "f",
"termSplitter" => ", ",
"lineStart" => "{",
"lineEnd" => "},",
"matrixStart" => "{",
"matrixEnd" => "}",
"comment" => "//",
"terminator" => ";",
),
"Julia"=>Dict(
"termsuffix" => "f0",
"termSplitter" => " ",
"lineStart" => "",
"lineEnd" => ";",
"matrixStart" => "[",
"matrixEnd" => "]",
"comment" => "#",
"terminator" => ";",
),
);
# Function to print Basic infomation
function PrintScriptVersion(f,s::String,v::String;codeStyle="C")
print(f,dictLanguage[codeStyle]["comment"]*" This is a piece of code generated by hypergraph\\GenshinDataConverter\n");
print(f,dictLanguage[codeStyle]["comment"]*" Code Style: "*codeStyle*"\n");
print(f,dictLanguage[codeStyle]["comment"]*" Script Version: "*s*"\n");
print(f,dictLanguage[codeStyle]["comment"]*" Your Version: "*v*"\n");
print(f,dictLanguage[codeStyle]["comment"]*" Time of Code Generation: "*Dates.format(now(), "yyyy-mm-dd HH:MM")*"\n");
end
# Function to print 2D Data
function Print2DMatrix(f,M;tabLen=0,varName="",codeStyle="C")
global dictLanguage
# print variableName if needed
if varName!=""
if codeStyle=="C"
print(f, "float "*varName);
for i in 1:length(size(M))
print(f,"["*string(size(M)[i])*"]");
end
print(f,"=");
elseif codeStyle=="Julia"
print(f,varName*"=");
else
error("Keyword codeStyle unsupported!")
end
end
# print the mainpart of 2D Matrix
print(f, repeat(" ",tabLen*4) * dictLanguage[codeStyle]["matrixStart"] * "\n");
for i in 1:size(M)[1]
print(f, repeat(" ", (tabLen+1)*4) * dictLanguage[codeStyle]["lineStart"]);
for j in 1:size(M)[2]
print(f, string(M[i,j]) * dictLanguage[codeStyle]["termsuffix"] * dictLanguage[codeStyle]["termSplitter"]);
end
print(f, dictLanguage[codeStyle]["lineEnd"] * "\n");
end
print(f, repeat(" ",tabLen*4) * dictLanguage[codeStyle]["matrixEnd"]);
# print Terminator
(varName!="")&&print(f, dictLanguage[codeStyle]["terminator"]);
(varName=="")&&print(f, dictLanguage[codeStyle]["termSplitter"]);
print(f,"\n");
end
# Function to print 3D Data
function Print3DMatrix(f,M;tabLen=0,varName="",codeStyle="C")
global dictlanguage
# print variableName if needed
if varName!=""
if codeStyle=="C"
print(f, "float "*varName);
for i in 1:length(size(M))
print(f,"["*string(size(M)[i])*"]");
end
print(f,"=");
elseif codeStyle=="Julia"
print(f,varName*"=");
else
error("Keyword codeStyle unsupported!")
end
end
# print the main part in different codeStyle
if codeStyle=="C"
print(f,"\n");
print(f,repeat(" ",tabLen*4) * "{\n");
for i in 1:size(M)[1]
Print2DMatrix(f,M[i,:,:];tabLen=tabLen+1,codeStyle="C")
end
print(f,"}");
elseif codeStyle=="Julia"
print("Array{Float32,3}(undef,"*string(size(M)[1])*","*string(size(M)[2])*","*string(size(M)[3])*");\n");
for i in 1:size(M)[1]
Print2DMatrix(f,M[i,:,:];varName=varName*"["*string(i)*",:,:]",codeStyle="Julia");
end
else
error("Keyword codeStyle unsupported!")
end
# print Terminator
(varName!="")&&(codeStyle=="C")&&print(f, dictLanguage[codeStyle]["terminator"]);
print(f,"\n");
end
# Function to print a Sturcture/Dictionary or such
# function PrintDict()
# end
end