-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
558909f
commit 3a0efa1
Showing
12 changed files
with
203 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package consts | ||
|
||
var ( | ||
const ( | ||
VERSION = `0.1.6` | ||
SIGNATURE = `LANG_LK` | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class table {} | ||
|
||
fn table.contains(tb, item) { | ||
for _, v in tb { | ||
if v == item { | ||
rt true | ||
} | ||
} | ||
rt false | ||
} | ||
|
||
fn table.len(tb) { | ||
i := 0 | ||
for _, _ in tb { | ||
i += 1 | ||
} | ||
rt i | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"vm": "0.1.6", | ||
"version": 2210261834 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package mods | ||
|
||
import ( | ||
"embed" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
|
||
"git.lolli.tech/lollipopkit/lk/consts" | ||
"git.lolli.tech/lollipopkit/lk/term" | ||
"git.lolli.tech/lollipopkit/lk/utils" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
var ( | ||
//go:embed index.json files | ||
ModFiles embed.FS | ||
LkEnv = os.Getenv("LK_PATH") | ||
|
||
indexFilePath = path.Join(LkEnv, "index.json") | ||
builtInIndexPath = "index.json" | ||
builtInFilesPath = "files" | ||
) | ||
|
||
func init() { | ||
if LkEnv == "" { | ||
term.Warn("env LK_PATH not set. \nCan't use built-in modules.") | ||
} | ||
} | ||
|
||
func InitMods() { | ||
if LkEnv == "" { | ||
return | ||
} | ||
if utils.Exist(indexFilePath) { | ||
indexBytes, err := ioutil.ReadFile(indexFilePath) | ||
if err != nil { | ||
term.Error("can't read index.json: " + err.Error()) | ||
} | ||
index := gjson.ParseBytes(indexBytes).Map() | ||
sameVM := index["vm"].String() == consts.VERSION | ||
version := index["version"].Int() | ||
bulitInIndexBytes, err := ModFiles.ReadFile(builtInIndexPath) | ||
if err != nil { | ||
term.Error("can't read built-in index.json: " + err.Error()) | ||
} | ||
builtInIndex := gjson.ParseBytes(bulitInIndexBytes).Map() | ||
builtInVersion := builtInIndex["version"].Int() | ||
if version >= builtInVersion && sameVM { | ||
return | ||
} | ||
} | ||
extract() | ||
} | ||
|
||
func extract() { | ||
term.Info("Extracting built-in modules...") | ||
index, err := ModFiles.ReadFile(builtInIndexPath) | ||
if err != nil { | ||
term.Error("can't read index.json: " + err.Error()) | ||
} | ||
err = os.WriteFile(indexFilePath, index, 0644) | ||
if err != nil { | ||
term.Error("can't write index.json: " + err.Error()) | ||
} | ||
files, err := ModFiles.ReadDir(builtInFilesPath) | ||
if err != nil { | ||
term.Error("can't read files: " + err.Error()) | ||
} | ||
|
||
for idx := range files { | ||
if files[idx].IsDir() { | ||
continue | ||
} | ||
data, err := ModFiles.ReadFile(path.Join(builtInFilesPath, files[idx].Name())) | ||
if err != nil { | ||
term.Error("can't read file: " + err.Error()) | ||
} | ||
err = os.WriteFile(path.Join(LkEnv, files[idx].Name()), data, 0644) | ||
if err != nil { | ||
term.Error("can't write file: " + err.Error()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package utils | ||
|
||
import ( | ||
"archive/zip" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func Unzip(src, dest string) error { | ||
r, err := zip.OpenReader(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if err := r.Close(); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
os.MkdirAll(dest, 0755) | ||
|
||
// Closure to address file descriptors issue with all the deferred .Close() methods | ||
extractAndWriteFile := func(f *zip.File) error { | ||
rc, err := f.Open() | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if err := rc.Close(); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
path := filepath.Join(dest, f.Name) | ||
|
||
// Check for ZipSlip (Directory traversal) | ||
if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) { | ||
return fmt.Errorf("illegal file path: %s", path) | ||
} | ||
|
||
if f.FileInfo().IsDir() { | ||
os.MkdirAll(path, f.Mode()) | ||
} else { | ||
os.MkdirAll(filepath.Dir(path), f.Mode()) | ||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if err := f.Close(); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
_, err = io.Copy(f, rc) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
for _, f := range r.File { | ||
err := extractAndWriteFile(f) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters