summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/wwwroot/Content/js/log-parser.js
blob: 8e30ae12e2f02edf3f781c6829a02921a85c1f38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* globals $ */

var smapi = smapi || {};
smapi.logParser = function(sectionUrl, pasteID) {
    /*********
    ** Initialisation
    *********/
    var stage,
        flags = $("#modflags"),
        output = $("#output"),
        filters = 0,
        memory = "",
        versionInfo,
        modInfo,
        modMap,
        modErrors,
        logInfo,
        templateBody = $("#template-body").text(),
        templateModentry = $("#template-modentry").text(),
        templateCss = $("#template-css").text(),
        templateLogentry = $("#template-logentry").text(),
        templateLognotice = $("#template-lognotice").text(),
        regexInfo = /\[[\d\:]+ INFO  SMAPI] SMAPI (.*?) with Stardew Valley (.*?) on (.*?)\n/g,
        regexMods = /\[[^\]]+\] Loaded \d+ mods:(?:\n\[[^\]]+\]    .+)+/g,
        regexLog = /\[([\d\:]+) (TRACE|DEBUG|INFO|WARN|ALERT|ERROR) ? ([^\]]+)\] ?((?:\n|.)*?)(?=(?:\[\d\d:|$))/g,
        regexMod = /\[(?:.*?)\] *(.*?) (\d+\.?(?:.*?))(?: by (.*?))? \|(?:.*?)$/gm,
        regexDate = /\[\d{2}:\d{2}:\d{2} TRACE SMAPI\] Log started at (.*?) UTC/g,
        regexPath = /\[\d{2}:\d{2}:\d{2} DEBUG SMAPI\] Mods go here: (.*?)(?:\n|$)/g;

    $("#tabs li:not(.notice)").on("click", function(evt) {
        var t = $(evt.currentTarget);
        t.toggleClass("active");
        $("#output").toggleClass(t.text().toLowerCase());
    });
    $("#upload-button").on("click", function() {
        memory = $("#input").val() || "";
        $("#input").val("");
        $("#popup-upload").fadeIn();
    });
    $("#popup-upload").on({
        'dragover dragenter': function(e) {
            e.preventDefault();
            e.stopPropagation();
        },
        'drop': function(e) {
            $("#uploader").attr("data-text", "Reading...");
            $("#uploader").show();
            var dataTransfer = e.originalEvent.dataTransfer;
            if (dataTransfer && dataTransfer.files.length) {
                e.preventDefault();
                e.stopPropagation();
                var file = dataTransfer.files[0];
                var reader = new FileReader();
                reader.onload = $.proxy(function(file, $input, event) {
                    $input.val(event.target.result);
                    $("#uploader").fadeOut();
                    $("#submit").click();
                }, this, file, $("#input"));
                reader.readAsText(file);
            }
        }
    });

    $("#submit").on("click", function() {
        $("#popup-upload").fadeOut();
        var paste = $("#input").val();
        if (paste) {
            memory = "";
            $("#uploader").attr("data-text", "Saving...");
            $("#uploader").fadeIn();
            $
                .ajax({
                    type: "POST",
                    url: sectionUrl + "/save",
                    data: JSON.stringify(paste),
                    contentType: "application/json" // sent to API
                })
                .fail(function(xhr, textStatus) {
                    $("#uploader").fadeOut();
                    $("#output").html('<div id="log" class="color-red"><h1>Parsing failed!</h1>Parsing of the log failed, details follow.<br />&nbsp;<p>Stage: Upload</p>Error: ' + textStatus + ': ' + xhr.responseText + "<hr /><pre>" + $("#input").val() + "</pre></div>");
                })
                .then(function(data) {
                    $("#uploader").fadeOut();
                    if (!data.success)
                        $("#output").html('<div id="log" class="color-red"><h1>Parsing failed!</h1>Parsing of the log failed, details follow.<br />&nbsp;<p>Stage: Upload</p>Error: ' + data.error + "<hr />" + $("#input").val() + "</div>");
                    else
                        location.href = (sectionUrl.replace(/\/$/, "") + "/" + data.id);
                });
        } else {
            alert("Unable to parse log, the input is empty!");
            $("#uploader").fadeOut();
        }
    });
    $("#cancel").on("click", function() {
        $("#popup-upload").fadeOut(400, function() {
            $("#input").val(memory);
            memory = "";
        });
    });
    $("#closeraw").on("click", function() {
        $("#popup-raw").fadeOut(400);
    });
    if (pasteID) {
        getData(pasteID);
    }
    else
        $("#popup-upload").fadeIn();


    /*********
    ** Helpers
    *********/
    function modClicked(evt) {
        var id = $(evt.currentTarget).attr("id").split("-")[1],
            cls = "mod-" + id;
        if (output.hasClass(cls))
            filters--;
        else
            filters++;
        output.toggleClass(cls);
        if (filters === 0) {
            output.removeClass("modfilter");
        } else {
            output.addClass("modfilter");
        }
    }

    function removeFilter() {
        for (var c = 0; c < modInfo.length; c++) {
            output.removeClass("mod-" + c);
        }
        filters = 0;
        output.removeClass("modfilter");
    }

    function selectAll() {
        for (var c = 0; c < modInfo.length; c++) {
            output.addClass("mod-" + c);
        }
        filters = modInfo.length;
        output.addClass("modfilter");
    }

    function parseData() {
        stage = "parseData.pre";
        var data = $("#input").val();
        if (!data) {
            stage = "parseData.checkNullData";
            throw new Error("Field `data` is null");

        }
        var dataInfo = regexInfo.exec(data) || regexInfo.exec(data) || regexInfo.exec(data),
            dataMods = regexMods.exec(data) || regexMods.exec(data) || regexMods.exec(data),
            dataDate = regexDate.exec(data) || regexDate.exec(data) || regexDate.exec(data),
            dataPath = regexPath.exec(data) || regexPath.exec(data) || regexPath.exec(data),
            match;
        stage = "parseData.doNullCheck";
        if (!dataInfo)
            throw new Error("Field `dataInfo` is null");
        if (!dataMods)
            throw new Error("Field `dataMods` is null");
        if (!dataPath)
            throw new Error("Field `dataPath` is null");
        dataMods = dataMods[0];
        stage = "parseData.setupDefaults";
        modMap = {
            "SMAPI": 0
        };
        modErrors = {
            "SMAPI": 0,
            "Console.Out": 0
        };
        logInfo = [];
        modInfo = [
            ["SMAPI", dataInfo[1], "Zoryn, CLxS & Pathoschild"]
        ];
        stage = "parseData.parseInfo";
        var date = dataDate ? new Date(dataDate[1] + "Z") : null;
        versionInfo = [dataInfo[1], dataInfo[2], dataInfo[3], date ? date.getFullYear() + "-" + ("0" + date.getMonth().toString()).substr(-2) + "-" + ("0" + date.getDay().toString()).substr(-2) + " at " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + " " + date.toLocaleTimeString("en-us", { timeZoneName: "short" }).split(" ")[2] : "No timestamp found", dataPath[1]];
        stage = "parseData.parseMods";
        while ((match = regexMod.exec(dataMods))) {
            modErrors[match[1]] = 0;
            modMap[match[1]] = modInfo.length;
            modInfo.push([match[1], match[2], match[3] ? ("by " + match[3]) : "Unknown author"]);
        }
        stage = "parseData.parseLog";
        while ((match = regexLog.exec(data))) {
            if (match[2] === "ERROR")
                modErrors[match[3]]++;
            logInfo.push([match[1], match[2], match[3], match[4]]);
        }
        stage = "parseData.post";
        modMap["Console.Out"] = modInfo.length;
        modInfo.push(["Console.Out", "", ""]);
    }

    function renderData() {
        stage = "renderData.pre";
        output.html(prepare(templateBody, versionInfo));
        var modslist = $("#modslist"), log = $("#log"), modCache = [], y = 0;
        for (; y < modInfo.length; y++) {
            var errors = modErrors[modInfo[y][0]],
                err, cls = "color-red";
            if (errors === 0) {
                err = "No Errors";
                cls = "color-green";
            } else if (errors === 1)
                err = "1 Error";
            else
                err = errors + " Errors";
            modCache.push(prepare(templateModentry, [y, modInfo[y][0], modInfo[y][1], modInfo[y][2], cls, err]));
        }
        modslist.append(modCache.join(""));
        for (var z = 0; z < modInfo.length; z++)
            $("#modlink-" + z).on("click", modClicked);
        var flagCache = [];
        for (var c = 0; c < modInfo.length; c++)
            flagCache.push(prepare(templateCss, [c]));
        flags.html(flagCache.join(""));
        var logCache = [], dupeCount = 0, dupeMemory = "|||";
        for (var x = 0; x < logInfo.length; x++) {
            var dm = logInfo[x][1] + "|" + logInfo[x][2] + "|" + logInfo[x][3];
            if (dupeMemory !== dm) {
                if (dupeCount > 0)
                    logCache.push(prepare(templateLognotice, [logInfo[x - 1][1].toLowerCase(), modMap[logInfo[x - 1][2]], dupeCount]));
                dupeCount = 0;
                dupeMemory = dm;
                logCache.push(prepare(templateLogentry, [logInfo[x][1].toLowerCase(), modMap[logInfo[x][2]], logInfo[x][0], logInfo[x][1], logInfo[x][2], logInfo[x][3].split("  ").join("&nbsp ").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br />")]));
            }
            else
                dupeCount++;
        }
        log.append(logCache.join(""));
        $("#modlink-r").on("click", removeFilter);
        $("#modlink-a").on("click", selectAll);
    }

    function prepare(str, arr) {
        var regex = /\{(\d)\}/g,
            match;
        while ((match = regex.exec(str)))
            str = str.replace(match[0], arr[match[1]]);
        return str;
    }
    function loadData() {
        try {
            stage = "loadData.Pre";
            var start = performance.now();
            parseData();
            renderData();
            var end = performance.now();
            $(".always").prepend("<div>Log processed in: " + (Math.round((end - start) * 100) / 100) + ' ms (<a id="viewraw" href="#">View raw</a>)</div><br />');
            $("#viewraw").on("click", function() {
                $("#dataraw").val($("#input").val());
                $("#popup-raw").fadeIn();
            });
            stage = "loadData.Post";
        }
        catch (err) {
            $("#output").html('<div id="log" class="color-red"><h1>Parsing failed!</h1>Parsing of the log failed, details follow.<br />&nbsp;<p>Stage: ' + stage + "</p>" + err + '<hr /><pre id="rawlog"></pre></div>');
            $("#rawlog").text($("#input").val());
        }
    }
    function getData(pasteID) {
        $("#uploader").attr("data-text", "Loading...");
        $("#uploader").fadeIn();
        $.get(sectionUrl + "/fetch/" + pasteID, function(data) {
            if (data.success) {
                $("#input").val(data.content);
                loadData();
            } else {
                $("#output").html('<div id="log" class="color-red"><h1>Fetching the log failed!</h1><p>' + data.error + '</p><pre id="rawlog"></pre></div>');
                $("#rawlog").text($("#input").val());
            }
            $("#uploader").fadeOut();
        });
    }
};