Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
L0laapk3 committed Dec 26, 2020
2 parents 62c5897 + 4fe948f commit 3479b2a
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 55 deletions.
115 changes: 70 additions & 45 deletions auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@

import psutil
from PIL import Image, ImageChops
from orderedset import OrderedSet

from crop import crop
from ref import ref
Expand All @@ -56,6 +55,11 @@

userFolder = Path(__file__, "..", "..", "..").resolve()

def naturalSort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('(\d+)', key) ]
return sorted(l, key = alphanum_key)

def printErase(arg):
try:
tsiz = tsize()[0]
Expand All @@ -65,15 +69,18 @@ def printErase(arg):
pass


def startGameAndReadGameLogs(results, condition, popenArgs, isSteam, tmpDir, pidBlacklist, rawTags, args):
def startGameAndReadGameLogs(results, condition, exeWithArgs, isSteam, tmpDir, pidBlacklist, rawTags, args):

pipeOut, pipeIn = os.pipe()
p = subprocess.Popen(popenArgs, stdout=pipeIn)
p = subprocess.Popen(exeWithArgs, stdout=pipeIn)

printingStackTraceback = False
# TODO: keep printing multiline stuff until new print detected
prevPrinted = False
def handleGameLine(line):
def handleGameLine(line, isFirst):
if isFirst and not re.match(r'^ *\d+\.\d{3} \d{4}-\d\d-\d\d \d\d:\d\d:\d\d; Factorio (\d+\.\d+\.\d+) \(build (\d+), [^)]+\)$', line):
raise Exception("Unrecognised output from factorio (maybe your version is outdated or too new?)\n\nOutput from factorio:\n" + line)

nonlocal prevPrinted
line = line.rstrip('\n')
if re.match(r'^\ *\d+(?:\.\d+)? *[^\n]*$', line) is None:
Expand All @@ -82,7 +89,7 @@ def handleGameLine(line):
return

prevPrinted = False

m = re.match(r'^\ *\d+(?:\.\d+)? *Script *@__L0laapk3_FactorioMaps__\/data-final-fixes\.lua:\d+: FactorioMaps_Output_RawTagPaths:([^:]+):(.*)$', line, re.IGNORECASE)
if m is not None:
rawTags[m.group(1)] = m.group(2)
Expand All @@ -107,11 +114,7 @@ def handleGameLine(line):


with os.fdopen(pipeOut, 'r') as pipef:
line = pipef.readline().rstrip("\n") if isSteam else ""
printingStackTraceback = handleGameLine(line)
if not isSteam and not re.match(r'^ *\d+\.\d{3} \d{4}-\d\d-\d\d \d\d:\d\d:\d\d; Factorio (\d+\.\d+\.\d+) \(build (\d+), [^)]+\)$', line):
raise Exception("Unrecognised output from factorio (maybe your version is outdated?)\n\nOutput from factorio:\n" + line)


if isSteam:
printErase("using steam launch hack.")

Expand Down Expand Up @@ -141,6 +144,7 @@ def handleGameLine(line):

psutil.Process(pid).nice(psutil.BELOW_NORMAL_PRIORITY_CLASS if os.name == 'nt' else 10)

isFirstLine = True
if isSteam:
pipef.close()
with Path(tmpDir, "factorio-current.log").open("r", encoding="utf-8") as f:
Expand All @@ -151,12 +155,14 @@ def handleGameLine(line):
time.sleep(0.4)
f.seek(where)
else:
printingStackTraceback = handleGameLine(line)
printingStackTraceback = handleGameLine(line, isFirstLine)
isFirstLine = False

else:
while True:
line = pipef.readline()
printingStackTraceback = handleGameLine(line)
line = pipef.readline().rstrip("\n")
printingStackTraceback = handleGameLine(line, isFirstLine)
isFirstLine = False


def checkUpdate(reverseUpdateTest:bool = False):
Expand Down Expand Up @@ -420,7 +426,7 @@ def kill(pid, onlyStall=False):
saveNames = args.savename or [foldername]
foldername = foldername.replace('*', '').replace('?', '')

saveGames = OrderedSet()
saveGames = set()
for saveName in saveNames:
saveNameEscaped = glob.escape(saveName).replace("[*]", "*")
globResults = list(saves.glob(saveNameEscaped))
Expand All @@ -433,15 +439,10 @@ def kill(pid, onlyStall=False):
for result in results:
saveGames.add(result.stem)

if args.verbose > 0:
print(f"Will generate snapshots for : {list(saveGames)}")
saveGames = naturalSort(list(saveGames))

windowsPaths = [
"Program Files/Factorio/bin/x64/factorio.exe",
"Games/Factorio/bin/x64/factorio.exe",
"Program Files (x86)/Steam/steamapps/common/Factorio/bin/x64/factorio.exe",
"Steam/steamapps/common/Factorio/bin/x64/factorio.exe",
]
if args.verbose > 0:
print(f"Will generate snapshots for : {saveGames}")

if args.factorio:
possibleFactorioPaths = [args.factorio]
Expand Down Expand Up @@ -571,12 +572,23 @@ def kill(pid, onlyStall=False):
steamApiPath = Path(factorioPath, "..", "steam_api64.so")

if steamApiPath.exists(): # chances are this is a steam install..
if os.name == "nt":
steamPath = Path(factorioPath, "..", "..", "..", "..", "..", "..", "steam.exe")
else:
steamPath = Path(factorioPath, "..", "..", "..", "..", "..", "..", "steam")
# try to find steam
try:
from winreg import OpenKey, HKEY_CURRENT_USER, ConnectRegistry, QueryValueEx, REG_SZ

key = OpenKey(ConnectRegistry(None, HKEY_CURRENT_USER), r'Software\Valve\Steam')
val, valType = QueryValueEx(key, 'SteamExe')
if valType != REG_SZ:
raise FileNotFoundError( errno.ENOENT, os.strerror(errno.ENOENT), "SteamExe")
steamPath = Path(val)
except (ImportError, FileNotFoundError) as e:
# fallback to old method
if os.name == "nt":
steamPath = Path(factorioPath, "..", "..", "..", "..", "..", "..", "steam.exe")
else:
steamPath = Path(factorioPath, "..", "..", "..", "..", "..", "..", "steam")

if steamPath.exists(): # found a steam executable
if steamPath and steamPath.exists(): # found a steam executable
usedSteamLaunchHack = True
exeWithArgs = [
str(steamPath),
Expand Down Expand Up @@ -726,20 +738,6 @@ def refZoom():
os.remove(os.path.join(workfolder, "mapInfo.out.json"))



print("updating labels")
tags = {}
with Path(workfolder, "mapInfo.json").open('r+', encoding='utf-8') as mapInfoJson:
data = json.load(mapInfoJson)
for mapStuff in data["maps"]:
for surfaceName, surfaceStuff in mapStuff["surfaces"].items():
if "tags" in surfaceStuff:
for tag in surfaceStuff["tags"]:
if "iconType" in tag:
tags[tag["iconType"] + tag["iconName"][0].upper() + tag["iconName"][1:]] = tag

rmtree(os.path.join(workfolder, "Images", "labels"), ignore_errors=True)

modVersions = sorted(
map(lambda m: (m.group(2).lower(), (m.group(3), m.group(4), m.group(5), m.group(6) is None), m.group(1)),
filter(lambda m: m,
Expand All @@ -751,13 +749,40 @@ def refZoom():

rawTags["__used"] = True
if args.tags:
for _, tag in tags.items():
dest = os.path.join(workfolder, tag["iconPath"])
os.makedirs(os.path.dirname(dest), exist_ok=True)
print("updating labels")
tags = {}
def addTag(tags, itemType, itemName, force=False):
index = itemType + itemName[0].upper() + itemName[1:]
if index in rawTags:
tags[index] = {
"itemType": itemType,
"itemName": itemName,
"iconPath": "Images/labels/" + itemType + "/" + itemName + ".png",
}
else:
if force:
raise "tag not found."
else:
print(f"[WARNING] tag \"{index}\" not found.")
with Path(workfolder, "mapInfo.json").open('r+', encoding='utf-8') as mapInfoJson:
data = json.load(mapInfoJson)
for mapStuff in data["maps"]:
for surfaceName, surfaceStuff in mapStuff["surfaces"].items():
if "tags" in surfaceStuff:
for tag in surfaceStuff["tags"]:
if "iconType" in tag:
addTag(tags, tag["iconType"], tag["iconName"], True)
if "text" in tag:
for match in re.finditer("\[([^=]+)=([^\]]+)", tag["text"]):
addTag(tags, match.group(1), match.group(2))

rmtree(os.path.join(workfolder, "Images", "labels"), ignore_errors=True)

rawPath = rawTags[tag["iconType"] + tag["iconName"][0].upper() + tag["iconName"][1:]]
for tagIndex, tag in tags.items():
dest = os.path.join(workfolder, tag["iconPath"])
os.makedirs(os.path.dirname(dest), exist_ok=True)

rawPath = rawTags[tagIndex]

icons = rawPath.split('|')
img = None
Expand Down
3 changes: 1 addition & 2 deletions data-final-fixes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ local function index(entity, type)
-- }


--TODO: handle barrels..
local path = ""
if entity.icon ~= nil then
path = entity.icon:sub(1, -5)
Expand Down Expand Up @@ -54,14 +53,14 @@ for _, signal in pairs(data.raw["virtual-signal"]) do
end

-- hopefully we dont have to hardcode this shit anymore in 0.17.. https://forums.factorio.com/viewtopic.php?f=28&t=64875
-- 1.1 update: lmao
for _, type in pairs({"item", "ammo", "capsule", "gun", "item-with-entity-data", "item-with-label", "item-with-inventory", "blueprint-book", "item-with-tags", "selection-tool", "blueprint", "deconstruction-item", "module", "rail-planner", "tool", "armor", "mining-tool", "repair-tool"}) do
for _, item in pairs(data.raw[type]) do
index(item, "item")
end
end

for _, fluid in pairs(data.raw["fluid"]) do
--TODO: handle barrels..
index(fluid)
end

Expand Down
2 changes: 1 addition & 1 deletion info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "L0laapk3_FactorioMaps",
"version": "4.1.1",
"version": "4.2.0",
"title": "FactorioMaps",
"author": "L0laapk3",
"contact": "https://github.com/L0laapk3/",
Expand Down
4 changes: 2 additions & 2 deletions json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function prettyjson(o, i)
elseif type(o) == 'boolean' then
return o and "true" or "false"
else
return '"'..tostring(o)..'"'
return '"'..tostring(o):gsub('"', '\\"')..'"'
end
end
function json(o, i)
Expand All @@ -28,6 +28,6 @@ function json(o, i)
elseif type(o) == 'boolean' then
return o and "true" or "false"
else
return '"'..tostring(o)..'"'
return '"'..tostring(o):gsub('"', '\\"')..'"'
end
end
3 changes: 1 addition & 2 deletions packages.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
PyTurboJPEG>=1.1.5
psutil>=5.4.8
numpy>=1.16.4
Pillow>=6.1.0
orderedset>=2.0.1
Pillow>=6.1.0
8 changes: 7 additions & 1 deletion updates.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,11 @@
"Fix for when the CWD is different than the script directory",
"Bugfixes :)"
],
"4.1.1": "Updated for 1.1"
"4.1.1": "Updated for 1.1",
"4.2.0": [
"Fix steam compatibility",
"Remove orderedset dependency",
"Add partial support for rich text in item labels",
"Security fix that prevents html injection"
]
}
5 changes: 5 additions & 0 deletions web/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ map-marker span {
display: block;
width: 96px;
}
map-marker span img {
height: 2em;
width: 2em;
margin: 0 .5em -.75em .5em;
}

map-marker.map-marker-default:before {
content: "";
Expand Down
5 changes: 3 additions & 2 deletions web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ for (let i = 0; i < mapInfo.maps.length; i++) {
layer.tags.sort((a, b) => a.position.y - b.position.y);
const mapInfoTimeLayer = Object.values(mapInfo.maps).find(m => m.path == map.path);
for (const tag of layer.tags) {

let label = {
surface: surface,
path: map.path,
Expand All @@ -140,7 +139,9 @@ for (let i = 0; i < mapInfo.maps.length; i++) {
icon: new L.DivIcon({
className: 'map-tag',
html: (tag.iconPath ? '<map-marker><img src="' + tag.iconPath + '"/>' : '<map-marker class="map-marker-default">') +
'<span>' + tag.text + '</span></map-marker>',
'<span>' + tag.text.replaceAll(/</g, "&lt;").replaceAll(/>/g, "&gt;").replaceAll(/\[([^=]+)=([^\]]+)\]/g, (a, type, name) => {
return '<img src="Images/labels/' + type + "/" + name + '.png">';
}) + '</span></map-marker>',
iconSize: null,
})
}),
Expand Down

0 comments on commit 3479b2a

Please sign in to comment.