Modding Discussion Powershell Script to catalog all json files

Discussion in 'Starbound Modding' started by Paragonus, Dec 22, 2013.

  1. Paragonus

    Paragonus Space Spelunker

    This is my first post so I might as well make it a good one.

    I started learning Powershell yesterday and thought I would make something for the Starbound Modding community. This script creates a catalog of the values in the JSON files. Running the script from your main Starbound directory will create a MainCatalog.html which has links to other files created in the HTML directory. I'm using the built-in powershell.exe in windows 8 which is version 3. You will need to set the execution policy to unrestricted because the script creates files.

    There are a few badly formatted JSON files that don't convert correctly that will show in the console while running. I wonder if Tiy would like to know which files they are?

    Let me know what you think. Excluding the CSS there is about 100 lines of code. I'm sure someone already made an executable that does the same thing and better. But I wanted to make something that people could play with and make changes to it. I should add comments to my code as good programmer should. Let me know if you want me to do that.

    Instructions:
    Create a file called CreateCatalog.ps1 from the larger code block below and put it in your Starbound directory. Then run powershell.exe, change the current directory to the Starbound directory, and run the following commands.

    Code:
    PS > $PSVersionTable
    PS > Set-ExecutionPolicy Unrestricted
    PS > .\CreateCatalog.ps1
    MainCatalog.html (pic)
    Catalog.PNG

    GeneratedGun.html (pic)
    CatalogItem.PNG

    Clicking on a link will take you to that file.
    CatalogItemDetails.PNG

    Code:
    $a = "
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <style>
    body {
        font-family: 'trebuchet MS', 'Lucida sans', Arial;
        font-size: 14px;
        color: #444;
    }
     
    table {
        *border-collapse: collapse; /* IE7 and lower */
        border-spacing: 0;
    }
     
    .bordered {
        border: solid #ccc 1px;
        -moz-border-radius: 6px;
        -webkit-border-radius: 6px;
        border-radius: 6px;
        -webkit-box-shadow: 0 1px 1px #ccc;
        -moz-box-shadow: 0 1px 1px #ccc;
        box-shadow: 0 1px 1px #ccc;       
    }
     
    .bordered tr:hover {
        background: #fbf8e9;
        -o-transition: all 0.1s ease-in-out;
        -webkit-transition: all 0.1s ease-in-out;
        -moz-transition: all 0.1s ease-in-out;
        -ms-transition: all 0.1s ease-in-out;
        transition: all 0.1s ease-in-out;   
    }   
       
    .bordered td, .bordered th {
        border-left: 1px solid #ccc;
        border-top: 1px solid #ccc;
        padding: 10px;
        text-align: left;   
    }
     
    .bordered th {
        background-color: #dce9f9;
        background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
        background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
        background-image:    -moz-linear-gradient(top, #ebf3fc, #dce9f9);
        background-image:    -ms-linear-gradient(top, #ebf3fc, #dce9f9);
        background-image:      -o-linear-gradient(top, #ebf3fc, #dce9f9);
        background-image:        linear-gradient(top, #ebf3fc, #dce9f9);
        -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
        -moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset; 
        box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;       
        border-top: none;
        text-shadow: 0 1px 0 rgba(255,255,255,.5);
    }
     
    .bordered td:first-child, .bordered th:first-child {
        border-left: none;
    }
     
    .bordered th:first-child {
        -moz-border-radius: 6px 0 0 0;
        -webkit-border-radius: 6px 0 0 0;
        border-radius: 6px 0 0 0;
    }
     
    .bordered th:last-child {
        -moz-border-radius: 0 6px 0 0;
        -webkit-border-radius: 0 6px 0 0;
        border-radius: 0 6px 0 0;
    }
     
    .bordered th:only-child{
        -moz-border-radius: 6px 6px 0 0;
        -webkit-border-radius: 6px 6px 0 0;
        border-radius: 6px 6px 0 0;
    }
     
    .bordered tr:last-child td:first-child {
        -moz-border-radius: 0 0 0 6px;
        -webkit-border-radius: 0 0 0 6px;
        border-radius: 0 0 0 6px;
    }
     
    .bordered tr:last-child td:last-child {
        -moz-border-radius: 0 0 6px 0;
        -webkit-border-radius: 0 0 6px 0;
        border-radius: 0 0 6px 0;
    }
    </style>
    </head>
    <body>"
    $b = $a + "<table class=bordered><tr><th>[columnname]</th></tr>"
     
    $extensions = @("2FUNCTIONS", "ANIMATION","AUGMENT","BACK","BARREN","BEAMAXE","BUSH","CANYON","CELESTIAL","CHASM",
    "CHEST","CINEMATIC","CODEX","CODEXITEM","COINITEM","CONFIG","CONFIGFUNCTIONS","CONSUMABLE","COREBIOME","DAMAGE",
    "DISABLED","DUNES","DUNGEON","EFFECTSOURCE","FLASHLIGHT","FRAMES","FUNCTIONS","GENERATEDGUN","GENERATEDSHIELD","GENERATEDSWORD",
    "GRAPPLINGHOOK","GRASS","GUN","HARVESTINGTOOL","HEAD","HILLS","INSTRUMENT","ISLANDS","ITEM","ITEMDESCRIPTION",
    "LEGS","MATERIAL","MATITEM","MATMOD","METADATA","MININGTOOL","MODULARFOLIAGE","MODULARSTEM","MONSTERPART","MONSTERSKILL",
    "MONSTERTYPE","MOUNTAINS","NPCTYPE","OBJECT","PAINTTOOL","PARALLAX","PARTICLE","PARTICLESOURCE","PROJECTILE","QUESTTEMPLATE",
    "RECIPE","RIDGEBLOCKS","RIDGECAVE","SPACEBIOME","SPECIES","STATUSEFFECT","STRUCTURE","SURFACEBIOME","SWORD","TECH",
    "TECHITEM","TERRAIN","THROWNITEM","TILLINGTOOL","TOOLTIP","TREASURECHESTS","TREASUREPOOLS","UNDERGROUNDBIOME","UNDERGROUNDPARALLAX",
    "UNUSEDMONSTERSKILL","WEATHER","WIRETOOL")
    $hashtable = @{}
    $sb = New-Object -TypeName "System.Text.StringBuilder"
    $fileshash = @{}
     
    cls;
    $b -replace "\[columnname\]", "Object Type" > "MainCatalog.html"
    $extensions | foreach {$extensionshtml += "<tr><td><a href=`"html/" + $_ + ".html`">" + $_ + "</a></td></tr>";}   
    $extensionshtml >> "MainCatalog.html"
    "</table>" >> "MainCatalog.html"
     
    new-item -name "HTML" -type "directory" -force > NULL;
     
    $extensions |
        foreach {
            $extension = $_;
            $outputfile = "HTML/" + $_ + ".html";
            $properties = @();
            $fileshash = @{};
            Get-ChildItem -Include @("*." + $_) -Recurse |
                foreach {
                    Write-Progress -activity ("Getting Data from " + $extension + "(s)") -currentOperation $_.Name;
                    $fileshash.Add($_.FullName, $_.Name);
                    $filename = $_.FullName;
                    $raw = Get-Content -path $_ -raw;
                    $raw = $raw -replace '(?m)/\*[^\*]+\*/', '' -replace '//.+', '';
                    $raw |                   
                        ConvertFrom-Json |
                            foreach {
                                $json = $_;
                                Get-Member -membertype "noteproperty" -inputobject $_ |
                                    foreach {
                                        if (-not ($properties -contains $_.Name)) {$properties += $_.Name;};
                                        $hashtable.Add(($filename + "_" + $_.Name), ($json.($_.Name) -join ", "));
                                };
                            };
                };
       
            $null = $sb.Append($a); $null = $sb.Append("<table class=bordered><thead><tr><th>File</th>");
            $total = ($properties.count * $fileshash.count);
            $curr = 0;
            $properties | Sort-Object | foreach {$null = $sb.Append("<th>" + $_ + "</th>")};
            $null = $sb.Append("</tr></thead>");
            $fileshash.GetEnumerator() | Sort-Object Value |
                foreach {
                    $filename = $_.Name;
                    $fileshortname = $_.Value;
                    $null = $sb.Append("<tr><td><a href=""file://");
                    $null = $sb.Append($filename);
                    $null = $sb.Append(""">");
                    $null = $sb.Append($fileshortname);
                    $null = $sb.AppendLine("</a></td>");
                    $properties | Sort-Object |
                        foreach {
                            $curr += 1;
                            $property = $_;
                            if (($curr % 100) -eq 0) {
                                Write-Progress -activity ("Creating Grid for " + $extension) -percentComplete (($curr/$total)*100);
                            };
                            $null = $sb.Append("<td>");
                            $null = $sb.Append($hashtable.Get_Item($filename + "_" + $property));
                            $null = $sb.Append("</td>");
                            $hashtable.Remove($filename + "_" + $property);
                        };
                    $null = $sb.AppendLine("</tr>");
                };
            $null = $sb.Append("</table></body></html>");
            $sb.ToString() > $outputfile;
            $sb.length = 0;
        }
     
  2. Magician Xy

    Magician Xy Ketchup Robot

    Looks cool. Couldn't one person run it, then post the results here?
     
    Nightmares likes this.

Share This Page