Initial commit

This commit is contained in:
Fierelier 2021-09-10 12:37:25 +02:00
commit 0a027ea0ef
3 changed files with 372 additions and 0 deletions

7
LICENSE.md Normal file
View File

@ -0,0 +1,7 @@
Copyright 2021
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

130
baseResource/client.lua Normal file
View File

@ -0,0 +1,130 @@
debugToggle = false
objectsToRegister = {}
objectsToPlace = {}
objectRegister = {}
modelRegister = {}
textureRegister = {}
blacklistObjects = {}
-- Blacklist for SanVice:
-- blacklistObjects[7479] = true
-- blacklistObjects[7316] = true
-- blacklistObjects[7317] = true
-- blacklistObjects[7318] = true
-- blacklistObjects[10904] = true
-- blacklistObjects[1205] = true
-- blacklistObjects[7324] = true
-- blacklistObjects[7319] = true
-- blacklistObjects[7320] = true
-- blacklistObjects[7321] = true
-- blacklistObjects[7322] = true
-- blacklistObjects[7323] = true
tempObj = false
logpath = "log.txt"
if fileExists(logpath) then fileDelete(logpath) end
logfile = fileCreate(logpath)
function displayStatus(text)
if debugToggle == false then return end
x,y = guiGetScreenSize()
dxDrawText(text .. " uwu",0,0,x,y,tocolor(0,255,0),1,"clear","center","center")
end
function debugWrite(text)
if debugToggle == false then return end
fileWrite(text)
fileFlush(logfile)
end
function registerObject(...) -- id,model,texture,drawDistance
table.insert(objectsToRegister,arg)
end
function registerObjectNow(id,model,texture,drawDistance)
if blacklistObjects[id] then
debugWrite("Skipping object " ..tostring(id).. " (" ..texture.. " + " ..model.. ") - Blacklisted.\n")
return
end
if textureRegister[texture] == nil then
debugWrite("Loading texture: " ..texture.. "\n")
textureRegister[texture] = engineLoadTXD(texture)
end
if modelRegister[model] == nil then
debugWrite("Loading model: " ..model.. "\n")
modelRegister[model] = engineLoadDFF(model)
end
realID = engineRequestModel("object",10768)
debugWrite("Making object " ..tostring(id).. ": " ..texture.. " + " ..model.. " = " ..tostring(realID).. "\n")
engineImportTXD(textureRegister[texture],realID)
engineReplaceModel(modelRegister[model],realID)
engineSetModelLODDistance(realID,drawDistance)
objectRegister[id] = realID
if tempObj then
destroyElement(tempObj)
end
if debugToggle == true then
x,y,z = getElementPosition(localPlayer)
tempObj = createObject(realID,x,y,z)
setElementCollisionsEnabled(tempObj,false)
end
displayStatus("Loaded model: " ..model.. ", " ..tostring(#objectsToRegister).. " remaining...")
end
function placeObject(...) -- id,interior,x,y,z,rx,ry,rz,lod
table.insert(objectsToPlace,arg)
end
function placeObjectNow(id,interior,x,y,z,rx,ry,rz,lod)
if objectRegister[id] == nil then return end
obj = createObject(objectRegister[id],x,y + 6000,z,rx,ry,rz)
setElementInterior(obj,interior)
setElementCollisionsEnabled(obj,false)
displayStatus("Placed model: " ..tostring(id).. ", " ..tostring(#objectsToPlace).. " remaining...")
end
function handleLoading()
if debugToggle == true then
if #objectsToRegister > 0 then
object = table.remove(objectsToRegister,1)
registerObjectNow(unpack(object))
return
end
else
while #objectsToRegister > 0 do
object = table.remove(objectsToRegister,1)
registerObjectNow(unpack(object))
end
end
if tempObj then
destroyElement(tempObj)
end
if debugToggle == true then
if #objectsToPlace > 0 then
object = table.remove(objectsToPlace,1)
placeObjectNow(unpack(object))
return
end
else
while #objectsToPlace > 0 do
object = table.remove(objectsToPlace,1)
placeObjectNow(unpack(object))
end
end
end
addEventHandler("onClientRender",root,handleLoading)
addEventHandler("onClientResourceStop",root,function(res)
if res == resource then
for _,value in pairs(objectRegister) do
engineFreeModel(value)
end
end
end)

235
sa2mta.py Normal file
View File

@ -0,0 +1,235 @@
import sys
import os
import json
import shutil
p = os.path.join
def gtaStripLine(line):
line = line.replace("\n","")
line = line.replace("\r","")
while len(line) > 0 and line[0] in " ":
line = line[1:]
while len(line) > 0 and line[-1] in " ":
line = line[:-1]
return line
def gtaProcessConfigLine(line,commentChars="#\n",spaceChars=" "):
lineList = []
lineListLength = 0
currentValue = ""
def addValue():
nonlocal currentValue
nonlocal lineList
nonlocal lineListLength
if currentValue == "": return
lineList.append(currentValue)
currentValue = ""
lineListLength += 1
for char in line:
if char in commentChars: break
if char == "\r": continue
if char in spaceChars:
addValue()
continue
currentValue += char
addValue()
return lineList
def gtaReadIde(file):
elements = {}
with open(file) as fileh:
section = False
for line in fileh:
line = gtaStripLine(line)
line = line.split("#",1)[0]
args = line.split(",")
index = 0
length = len(args)
if length == 0: continue
while index < length:
args[index] = gtaStripLine(args[index])
index += 1
while length > 0 and args[-1] == "":
args = args[:-1]
length -= 1
if length == 0: continue
if length == 1:
section = args[0].lower()
if section == "end": section = False
continue
if section == False:
print("Malformed IDE - No section.")
continue
element = {}
if section == "objs":
element["id"] = args[0]
element["modelName"] = args[1]
element["textureName"] = args[2]
element["objectCount"] = 1
element["drawDistances"] = [args[3]]
element["flags"] = args[4]
if len(args) > 5:
element["objectCount"] = args[3]
element["drawDistances"] = args[4:4 + int(element["objectCount"])]
element["flags"] = args[4 + int(element["objectCount"])]
if not section in elements: elements[section] = []
if element != {}:
elements[section].append(element)
return elements
def gtaReadIpl(file):
elements = {}
with open(file) as fileh:
section = False
for line in fileh:
line = gtaStripLine(line)
line = line.split("#",1)[0]
args = line.split(",")
index = 0
length = len(args)
if length == 0: continue
while index < length:
args[index] = gtaStripLine(args[index])
index += 1
if length == 1:
section = args[0].lower()
if section == "end": section = False
continue
if section == False:
print("Malformed IPL - No section.")
continue
element = {}
if section == "inst":
element["id"] = args[0]
element["modelName"] = args[1]
element["interior"] = args[2]
element["position"] = [args[3],args[4],args[5]]
element["rotation"] = [args[6],args[7],args[8],args[9]]
if length < 11:
element["lod"] = "-1"
else:
element["lod"] = args[10]
if not section in elements: elements[section] = []
if element != {}:
elements[section].append(element)
return elements
def gtaReadDat(filePath,output={}):
global pathSA
with open(filePath,"r") as fileDat:
for line in fileDat:
line = gtaStripLine(line)
lineList = gtaProcessConfigLine(line)
if len(lineList) < 1: continue
if len(lineList) < 2:
print("Malformed line in gta.dat: " +line)
continue
dataType = lineList[0].lower()
dataPath = lineList[1].lower()
dataPathFull = p(pathSA,dataPath)
if dataType == "ide":
if not dataType in output: output[dataType] = {}
if not dataPath in output[dataType]:
output[dataType][dataPath] = gtaReadIde(dataPathFull)
if dataType == "ipl":
if not dataType in output: output[dataType] = {}
if not dataPath in output[dataType]:
output[dataType][dataPath] = gtaReadIpl(dataPathFull)
return output
def main():
global pathSA
pathSA = input("Path to San Andreas: ").replace("'","").replace('"',"")
pathGtaDat = p(pathSA,"data","gta.dat")
pathDefaultDat = p(pathSA,"data","default.dat")
pathOut = False
while pathOut == False or os.path.isdir(pathOut):
pathOut = input("Path to save resource in: ").replace("'","").replace('"',"")
shutil.copytree("baseResource",pathOut)
print("\n-- Interpreting files... --")
data = gtaReadDat(pathGtaDat)
data = gtaReadDat(pathDefaultDat,data)
print("Writing raw definitions...")
with open(p(pathOut,"raw.json"),"w") as logfile:
logfile.write(json.dumps(data, indent=2))
print("-- Re-interpreting and writing resource... --")
fileMeta = open(p(pathOut,"meta.xml"),"w")
fileMeta.write("<meta>\n")
fileMeta.write('<info author="unknown" version="1.0.0" name="' +pathOut.rsplit(os.path.sep,1)[-1]+ '"/>\n')
fileMeta.write('<script src="client.lua" type="client"/>\n')
for ide in data["ide"]:
fileMeta.write('<script src="gta/' +ide.replace("\\","/")+ '.lua" type="client"/>\n')
for ipl in data["ipl"]:
fileMeta.write('<script src="gta/' +ipl.replace("\\","/")+ '.lua" type="client"/>\n')
objectIDs = []
for ipl in data["ipl"]:
pathIpl = p(pathOut,"gta",ipl.replace("\\",os.path.sep) + ".lua")
try:
os.makedirs(os.path.dirname(pathIpl))
except:
pass
with open(pathIpl,"w") as fileIpl:
for section in data["ipl"][ipl]:
if section == "inst":
for element in data["ipl"][ipl][section]:
if not element["id"] in objectIDs:
objectIDs.append(element["id"])
fileIpl.write('placeObject(' +element["id"]+ ',' +element["interior"]+ ',' +element["position"][0]+ ',' +element["position"][1]+ ',' +element["position"][2]+ ',' +element["rotation"][0]+ ',' +element["rotation"][1]+ ',' +element["rotation"][2]+ ',' +element["lod"]+ ')\n')
modelNames = []
textureNames = []
for ide in data["ide"]:
pathIde = p(pathOut,"gta",ide.replace("\\",os.path.sep) + ".lua")
try:
os.makedirs(os.path.dirname(pathIde))
except:
pass
with open(pathIde,"w") as fileIde:
for section in data["ide"][ide]:
if section == "objs":
for element in data["ide"][ide][section]:
if not element["id"] in objectIDs: continue
fileIde.write('registerObject(' +element["id"]+ ',"img/' +element["modelName"]+ '.dff","img/' +element["textureName"]+ '.txd",' +element["drawDistances"][0]+ ')\n')
if not element["modelName"] in modelNames:
fileMeta.write('<file src="img/' +element["modelName"]+ '.dff"/>\n')
modelNames.append(element["modelName"])
if not element["textureName"] in textureNames:
fileMeta.write('<file src="img/' +element["textureName"]+ '.txd"/>\n')
textureNames.append(element["textureName"])
fileMeta.write("</meta>\n")
fileMeta.close()
main()