Generic Array Generator

Hydro

Civil Gamers Expert
Dec 19, 2020
216
125
71
Hi All!

@Remmy was screaming in my ear about needing resizable arrays in MNScript, so I've written a nice little array generator you can use and extend. At the moment it just supports primitives, but it should be relatively easy to expand. I'm providing the source code here, in-case you want to modify the templated array for whatever reason.

If you decide to generate the arrays from source, just put the two files in the same directory and run `ArrayGenerator.msc`. It should emit an `Arrays.mscx` file that you can use in your programs.
Code:
Arrays.mscx:
24
{"compiler_version":2.0}
vDql2OGRJHSsu4BSzStKW4TRA7JBoZYHxLFKJE7eN/qIusD9DPvTGlhqKxWf8Rbq
K+csazXJrODQiO/o7CuHwrj77Qx9DJpqAR0voqvfhr7KHuIOrUzVfCpmEznOnXUK
BPGEiygR6SfpQk5SBIKTZNefzwVPCPhsAZWmwpkIJQLoqf/oICzoOBte9FsI0+P3
TlE9EC0cxwhd4uisLiojjJjZWrQzGeqhd1aED4duFm/ndlZH5AXNmvuzP62AexQ6
KzazHMOo2STdPNxZTvLRlYqS6W0sV/pSLGv1miD0rQqyN3jYGWzZb0eKsAglon1D
/JLBOVAIagWZB2LBUTdAaRChOkvsAn15JW2r+G/GxgTmrYTOTqtF15KQFL489BDO
dLxz7LaRgYNc/lAGkqSOpZL/O7P+EYDZVXzxXpPQMqlkUyOV8wqtYUnR/rKOklDb
+s0dNnkWKAACiRL8RzMPQieNqnjRZNL+Z4dOcrlTarSPZUEPKzxng94SEJk+TXA4
hwNEoStXdLHyydoAyrEsvyHwuy57mAS/9bQNbUqajESfucWnemQdOYAgvlR9gGxQ
PlibQIms9czcIbCdc+tnUzJiVnr05hPVSd1gyTKPLyV4gGpdvZLa6zXavhnFmmxD
wt0WBnu3EY/MlQ7ywzVyqhDSMBJDKrYLTc+fuZoQeSkmyyjk/s+xJ8K9sGPzbLcw
Xv5GP7w3XXUN6zrUEjjYdorpXXfwa44FTV57rB468GQ+Qygfcxhv9dymUiCjoNmG
6ClADYjxHmx6E6805jiy6gSQeUJPNAx0OaXLeVquxI2fvBzZ7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oB
zXr8xTgP6OKg+3b+kwTj/GQDcKA0XK5HBi2rGOjLWmekCpw71K6LS5gSucVYJA6U
NpvvwyRL75nWuFWFiLbue5ntdYDensUhw2si/iwTwQhZGzOvEqAib2swqEW2XIeI
7jPOiGwhRE8twT+QAuhy9c6IWyHhr4YSsw6PM7jYYkB9TYXObQxLcmf2tRMPzYbV
mbC2ykGQl0ZeUuMuEAskWzEAy0jlN6tcoxN92Ylrv0FfnyK61uCRSNU4zHyOs+iU

Source:
C-like:
using Console;
using Array;

class TemplateType {}

class TemplateClassTypeArray {
    TemplateType[] _data = new TemplateType[2];
    TemplateType _default;
    number _length = 0;
    number _capacity = 2;
 
    constructor() {
        _default = _data[1];
    }
 
    function<number> GetLength() { return _length; }
    function<TemplateType[]> GetBuffer() { return _data; }
 
    function Add(TemplateType item) {
        number i = _length + 1;
    
        if (i > _capacity) {
            ResizeArray(_length * 2);
        }
    
        _length = _length + 1;
        _data[_length] = item;
    }
 
    function<bool> Remove(TemplateType item) {
        number idx = Array.IndexOf(_data, item);
    
        return RemoveAtIndex(idx);
    }
 
    function<bool> RemoveAtIndex(number idx) {
        if (idx > _length || idx <= 0) {
            return false;
        }
    
        number i = idx;
        number next = 0;
    
        while (i < _length) {
            next = i + 1;
        
            _data[i] = _data[next];
        
            i = next;
        }
    
        _data[_length] = _default;
        _length = _length - 1;
        return true;
    }
 
    function<bool> Contains(TemplateType item) {
        return Array.Contains(_data, item);
    }
 
    function ResizeArray(number newCapacity) {
        TemplateType[] newArray = new TemplateType[newCapacity];
        number i = 0;
    
        while (i < _capacity) {
            i = i + 1;
            newArray[i] = _data[i];
        }
    
        _capacity = newCapacity;
        _data = newArray;
    }
 
    function Print() {
        number i = 0;
    
        while (i < _capacity) {
            i=i+1;
            Console.WriteLine(i..": ".._data[i]);
        }
    }
}
C-like:
using System;
using Console;
using Event;
using String;
using File;
using Application;
using Array;

Application.RequestAdminPrivilege();

StringResult templateSourceCode = File.Read(Console.GetCurrentDirectory(), "Template.msc");

if (templateSourceCode.GetResult() == false) {
    Application.Error("Source code is missing for Template.msc");
}

function<string> GetTypeName(string type) {
    string firstChar = String.CharAt(type, 1);
    return String.Upper(firstChar)..String.Sub(type, 2, String.Length(type));
}

function ReplaceTemplateType(string type) {
 
}

function<string> GetSourceCode(string rawTypeName) {
    string copy = ""..templateSourceCode.GetString();
 
    copy = String.Replace(copy, "class TemplateType {}", "");
    copy = String.Replace(copy, "TemplateType", rawTypeName);
    copy = String.Replace(copy, "TemplateClassType", GetTypeName(rawTypeName));

    return copy;
}

function GenerateArrays(string[] types) {
    string combined = "";
    bool isFirst = true;
 
    number length = Array.Length(types);
    number i = 0;
    while (i < length) {
    
        i = i + 1;
        string type = types[i];
        string code = GetSourceCode(type);

        if (isFirst == false) {
            code = String.Replace(code, "using", "// using");
        }

        isFirst = false;   
        combined = combined.."\n"..code;
    }
 
    CompileFile("Arrays", combined);
}

function GenerateArrayStandalone(string typeName) {
 
    string copy = GetSourceCode(typeName);
    string upperTypeName = GetTypeName(typeName);
 
    // Make sure array starts with char
 
    CompileFile(upperTypeName.."Array", copy);
}

function CompileFile(string fileName, string code) {
 
    string here = Console.GetCurrentDirectory();
 
    File.Delete(here, fileName.."mscx");
    File.Write(here, fileName..".msc", code);
 
    System.RunCommand("mnscript compile "..fileName..".msc "..fileName);
 
    while (File.Exists(here, fileName..".mscx") == false) {
        System.Sleep(0.1);
    }
 
    Console.WriteLine("Finished compiling.....");
    File.Delete(here, fileName..".msc");
    System.Sleep(1);
}

GenerateArrays(["number", "string", "bool"]);
 
Last edited:
  • Love
Reactions: Ellie