I have this file named "test.json"
"AFMU repairs": {
"enabled": true,
"priority": 3,
"responder": true,
"script": "{event.item} \r\n{if event.repairedfully:\r\n fully repaired\r\n|else:\r\n partially repaired \r\n {Occasionally(2, cat(\r\n OneOf(\"to \", \"at \"),\r\n Humanise(event.health * 100),\r\n \" percent functionality\"\r\n ))}\r\n}\r\n\r\n{Occasionally(2, \r\n cat(OneOf(\", \", \"and is\"), \" ready for re-activation\")\r\n)}.",
"default": true,
"name": "AFMU repairs",
"description": "Triggered when repairing modules using the Auto Field Maintenance Unit (AFMU)"
},
"Asteroid cracked": {
"enabled": true,
"priority": 3,
"responder": true,
"script": null,
"default": true,
"name": "Asteroid cracked",
"description": "Triggered when you break up a 'Motherlode' asteroid for mining"
},
"Asteroid prospected": {
"enabled": true,
"priority": 3,
"responder": true,
"script": "{set minimumPercent to 10} {_ The minimum percentage surface mineral concentration to report _}\r\n{set spokenCores to [\r\n \"Alexandrite\": false,\r\n \"Benitoite\": false,\r\n \"Grandidierite\": false,\r\n \"Low Temperature Diamonds\": true,\r\n \"Monazite\": false,\r\n \"Musgravite\": false,\r\n \"Rhodplumsite\": false,\r\n \"Serendibite\": false,\r\n \"Void Opals\": true,\r\n]}\r\n{set spokenMinerals to [\r\n \"Bauxite\": false,\r\n \"Bertrandite\": false,\r\n \"Bromellite\": false,\r\n \"Cobalt\": false,\r\n \"Coltan\": false,\r\n \"Cryolite\": false,\r\n \"Gallite\": false,\r\n \"Gold\": false,\r\n \"Goslarite\": false,\r\n \"Hydrogen Peroxide\": false,\r\n \"Indite\": false,\r\n \"Jadeite\": false,\r\n \"Lepidolite\": false,\r\n \"Lithium Hydroxide\": false,\r\n \"Liquid oxygen\": false,\r\n \"Low Temperature Diamonds\": true,\r\n \"Methane Clathrate\": false,\r\n \"Methanol Monohydrate\": false,\r\n \"Moissanite\": false,\r\n \"Osmium\": false,\r\n \"Painite\": true,\r\n \"Platinum\": false,\r\n \"Palladium\": false,\r\n \"Praseodymium\": false,\r\n \"Pyrophyllite\": false,\r\n \"Rutile\": false,\r\n \"Samarium\": false,\r\n \"Silver\": false,\r\n \"Taaffeite\": false,\r\n \"Thorium\": false,\r\n \"Tritium\": true,\r\n \"Uraninite\": false,\r\n \"Water\": false,\r\n]}\r\n\r\n{if len(event.motherlode) > 0 && spokenCores[event.motherlode]:\r\n Motherlode detected: {event.motherlode}.\r\n}\r\n\r\n{set minerals to []}\r\n{for mineral in event.commodities:\r\n {if mineral.percentage > minimumPercent && spokenMinerals[mineral.commodity]:\r\n {set mineral_desc to: \r\n {round(mineral.percentage)} percent {mineral.commodity}\r\n }\r\n {set minerals to cat(minerals, [mineral_desc])}\r\n }\r\n}\r\n{if len(minerals) > 0:\r\n Asteroid contains {List(minerals)}\r\n {if event.materialcontent = \"High\":\r\n and a high concentration of engineering materials\r\n }.\r\n {if event.remaining < 100:\r\n It is {100 - event.remaining} percent depleted.\r\n }\r\n}",
"default": true,
"name": "Asteroid prospected",
"description": "Triggered when using a prospecting drone"
},
What i want is to "extract" the script string, make it readable, put the script name at the top and save a readable file "test.cottle".
-EDIT-
Below an example of the final result (converted only the first of the three script in test.json):
{_ AFMU repairs }
{event.item}
{if event.repairedfully:
fully repaired
|else:
partially repaired
{Occasionally(2, cat(
OneOf(\"to \", \"at \"),
Humanise(event.health * 100),
\" percent functionality\"
))}
}
{Occasionally(2,
cat(OneOf(\", \", \"and is\"), \" ready for re-activation\")
)}.
{_ Asteroid prospected }
{set minimumPercent to 10} {_ The minimum percentage surface mineral concentration to report _}
...
... and so on
Here's a working sample of the first expression.
I already have done it in SublimeText using regex search/replace, but i want to make it automatically, so i tried with PowerShell 7, which is basically a totally new thing for me.
This is my test.ps1:
Write-Host "Reading $FileName..."
$content = [System.IO.File]::ReadAllText("C:\test.json")
Write-Host "..replacing..."
$content -replace '(?sm)(.|\n)*?\"script\"\: \"(?<TheScript>.*)\"\,\n.*\n.*\"name\"\: \"(?<TheScriptName>.*)\".*\n' , '\{_ ${TheScriptName} \}\n${TheScript}\n\n'
$content -replace '\\r\\n' , '\n'
Write-Host "...saving."
[System.IO.File]::WriteAllText("C:\test.json.cottle", $content)
Well, the save file is just like the original.
Why does my replace won't work? i tried it at https://regexr.com/ and there's working (altough without named capturing groups). I also added the (?sm) from this question but it does'nt work. What should i do?