149 lines
3.3 KiB
Go
149 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"regexp"
|
|
|
|
"golang.design/x/clipboard"
|
|
)
|
|
|
|
var Error *log.Logger
|
|
var Warning *log.Logger
|
|
|
|
const CONFIG_FILE = `../config/barter.json`
|
|
const LOCALE_FILE = `C:\Games\Escape from Tarkov\Escape from Tarkov\SPT_Data\Server\database\locales\global\en.json`
|
|
|
|
func init() {
|
|
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
|
logFile, err := os.Create("main.log")
|
|
if err != nil {
|
|
log.Printf("Error creating log file: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
logger := io.MultiWriter(os.Stdout, logFile)
|
|
log.SetOutput(logger)
|
|
|
|
Error = log.New(io.MultiWriter(logFile, os.Stderr, os.Stdout),
|
|
fmt.Sprintf("%sERROR:%s ", "\033[0;101m", "\033[0m"),
|
|
log.Lmicroseconds|log.Lshortfile)
|
|
Warning = log.New(io.MultiWriter(logFile, os.Stdout),
|
|
fmt.Sprintf("%sWARNING:%s ", "\033[0;103m", "\033[0m"),
|
|
log.Lmicroseconds|log.Lshortfile)
|
|
}
|
|
|
|
var dataRegex = regexp.MustCompile(`Tpl: (.+?)\n`)
|
|
|
|
func main() {
|
|
file, err := os.Open(LOCALE_FILE)
|
|
if err != nil {
|
|
Error.Printf("Error opening items file: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
var locales Locale
|
|
err = json.NewDecoder(file).Decode(&locales)
|
|
if err != nil {
|
|
Error.Printf("Error decoding locale file: %v", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
db := &DB{
|
|
path:"cache.db",
|
|
}
|
|
db.Open()
|
|
cache := Cache{
|
|
db: db,
|
|
}
|
|
cache.Init()
|
|
cache.Tidy()
|
|
|
|
ch := clipboard.Watch(context.TODO(), clipboard.FmtText)
|
|
for data := range ch {
|
|
go func(data []byte) {
|
|
stringData := string(data)
|
|
// log.Println(stringData)
|
|
matches := dataRegex.FindStringSubmatch(stringData)
|
|
if len(matches) < 1 {
|
|
Warning.Printf("No matches found in: %s", stringData)
|
|
return
|
|
}
|
|
tpl := matches[1]
|
|
config := readConfig()
|
|
|
|
log.Printf("Creating new entry: %s", tpl)
|
|
newEntry := Entry{
|
|
Name: "",
|
|
Id: tpl,
|
|
Props: EntryProps{
|
|
StackSize: 100,
|
|
},
|
|
}
|
|
|
|
name, ok := locales[tpl+" Name"]
|
|
if ok {
|
|
log.Printf("Name found for %s as %s", tpl, name)
|
|
newEntry.Name = name
|
|
} else {
|
|
Warning.Printf("Name not found for %s", tpl)
|
|
}
|
|
|
|
val, err := cache.Get(tpl)
|
|
if err == nil {
|
|
log.Printf("%s%s already registered on %v!%s", On_IGreen, val.Value, val.Date, Reset)
|
|
}
|
|
cache.Set(tpl, name)
|
|
|
|
for _, entry := range config.List {
|
|
if entry.Id == tpl {
|
|
Warning.Printf("Entry already exists: %s", tpl)
|
|
return
|
|
}
|
|
}
|
|
|
|
config.List = append(config.List, newEntry)
|
|
writeConfig(config)
|
|
log.Printf("Config saved for: %s", tpl)
|
|
}(data)
|
|
}
|
|
}
|
|
|
|
func readConfig() *Config {
|
|
config := &Config{}
|
|
|
|
file, err := os.Open(CONFIG_FILE)
|
|
if err != nil {
|
|
Error.Printf("Error opening config file: %v", err)
|
|
return config
|
|
}
|
|
defer file.Close()
|
|
|
|
err = json.NewDecoder(file).Decode(&config)
|
|
if err != nil {
|
|
Error.Printf("Error decoding config file: %v", err)
|
|
return config
|
|
}
|
|
return config
|
|
}
|
|
|
|
func writeConfig(config *Config) {
|
|
file, err := os.Create(CONFIG_FILE)
|
|
if err != nil {
|
|
Error.Printf("Error creating config file: %v", err)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
encoder := json.NewEncoder(file)
|
|
encoder.SetIndent("", "\t")
|
|
err = encoder.Encode(config)
|
|
if err != nil {
|
|
Error.Printf("Error encoding config file: %v", err)
|
|
return
|
|
}
|
|
}
|