Skip to content

Commit

Permalink
profiles refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kociumba committed Oct 10, 2024
1 parent 89ecc15 commit 705c9b6
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 178 deletions.
2 changes: 1 addition & 1 deletion .task/checksum/build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4b0bf220c6b1a17111459c48c118e677
18f2d39cff8b43ed829b9b6dac85391e
2 changes: 1 addition & 1 deletion FyneApp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Website = "https://github.com/kociumba/Kinjector"
Name = "Kinjector"
ID = "org.kociumba.kinjector"
Version = "1.0.0"
Build = 58
Build = 60
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tasks:
- task: deps:install
- task: pre:build
- go mod tidy
- fyne package -os windows -icon assets/icon.png -name Kinjector --tags 'ldflags=-s -w' -appVersion {{.VERSION}}
- fyne package -os windows -icon assets/icon.ico -name Kinjector --tags 'ldflags=-s -w' -appVersion {{.VERSION}}
- task: find:and:move:bin

# bugged couse fyne is a piece of shit and assumes every one has a cert to sign release version
Expand Down
Binary file added assets/icon.ico
Binary file not shown.
183 changes: 10 additions & 173 deletions injector.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"image/color"
Expand All @@ -28,9 +27,13 @@ import (
var (
dbg = flag.Bool("dbg", false, "")

configDir string
configFile string
settingsFile string
configDir string
configFile string
settingsFile string
userSelection = &UserSelection{}
suppressSuggestions bool
procSelect *xwidget.CompletionEntry
selectDllButton *widget.Button
)

func init() {
Expand Down Expand Up @@ -58,87 +61,6 @@ type InjectionProfile struct {
DllFile string `json:"dllFile"`
}

type UserSelection struct {
SelectedProc string
SelectedDll string
DllFile string
processNames []string
Profiles []InjectionProfile
UnsafeUnload bool
}

func (u *UserSelection) SaveProfile(name string) error {
profile := InjectionProfile{
Name: name,
SelectedProc: u.SelectedProc,
SelectedDll: u.SelectedDll,
DllFile: u.DllFile,
}

u.Profiles = append(u.Profiles, profile)
return u.saveProfilesToFile()
}

func (u *UserSelection) LoadProfile(name string) error {
for _, profile := range u.Profiles {
if profile.Name == name {
u.SelectedProc = profile.SelectedProc
u.SelectedDll = profile.SelectedDll
u.DllFile = profile.DllFile
return nil
}
}
return fmt.Errorf("profile not found")
}

func (u *UserSelection) saveProfilesToFile() error {
data, err := json.Marshal(u.Profiles)
if err != nil {
return err
}

err = os.WriteFile(configFile, data, 0600)
if err != nil {
return err
}

// err = u.loadProfilesFromFile()
// if err != nil {
// return err
// }

return nil
}

func (u *UserSelection) loadProfilesFromFile() error {
data, err := os.ReadFile(configFile)
if err != nil {
if os.IsNotExist(err) {
// File doesn't exist, which is fine for first run
return nil
}
return err
}

err = json.Unmarshal(data, &u.Profiles)
if err != nil {
return err
}

return nil
}

func (u *UserSelection) DeleteProfile(name string) error {
for i, profile := range u.Profiles {
if profile.Name == name {
// Remove the profile from the slice
u.Profiles = append(u.Profiles[:i], u.Profiles[i+1:]...)
return u.saveProfilesToFile()
}
}
return fmt.Errorf("profile not found")
}

func trimFilePath(path string) string {

clog.Info("Selected dll: " + path)
Expand Down Expand Up @@ -176,7 +98,6 @@ func performUnload(userSelection *UserSelection, w fyne.Window) {
func main() {
flag.Parse()

userSelection := &UserSelection{}
err := userSelection.loadProfilesFromFile()
if err != nil {
clog.Warn("Failed to load profiles:", err)
Expand Down Expand Up @@ -275,8 +196,7 @@ func main() {
userSelection.processNames = initialProcessNames

// register the process selection input
procSelect := xwidget.NewCompletionEntry(userSelection.processNames)
var suppressSuggestions bool
procSelect = xwidget.NewCompletionEntry(userSelection.processNames)

procSelect.OnChanged = func(s string) {
if !suppressSuggestions {
Expand Down Expand Up @@ -311,22 +231,15 @@ func main() {
CreditsWindow(fyne.CurrentApp(), fyne.NewSize(800, 400)).Show()
})

// register text displays
dllDisplay := widget.NewLabelWithStyle("Dll selected: ", fyne.TextAlignLeading, fyne.TextStyle{Bold: true})
// errorDisplay := widget.NewLabelWithStyle("", fyne.TextAlignLeading, fyne.TextStyle{Bold: true})

injection := widget.NewActivity()

profileName := widget.NewEntry()
profileName.SetPlaceHolder("Profile Name")

// Create the unsafe unload checkbox
unsafeUnloadCheck := widget.NewCheck("Unsafe Unload (Use with caution)", func(checked bool) {
userSelection.UnsafeUnload = checked
})

// Damn this shit stupid
selectDllButton := widget.NewButton("", func() {})
selectDllButton = widget.NewButton("", func() {})

selectDllButton = widget.NewButton("Select DLL", func() {
userSelection.SelectedDll, err = zenity.SelectFile(zenity.Filename(os.ExpandEnv("$HOME")), zenity.FileFilter{Patterns: []string{"*.dll"}})
Expand Down Expand Up @@ -434,83 +347,7 @@ func main() {
nil,
)

// Create the profile management tab
profileName = widget.NewEntry()
profileName.SetPlaceHolder("Profile Name")

loadProfileSelect := widget.NewSelect([]string{}, func(name string) {
err := userSelection.LoadProfile(name)
if err != nil {
dialog.ShowError(err, w)
} else {
suppressSuggestions = true
procSelect.SetText(userSelection.SelectedProc)
dllDisplay.SetText("Dll selected: " + userSelection.DllFile)
selectDllButton.SetText("DLL: " + userSelection.DllFile)
dialog.ShowInformation("Loaded profile", "Profile "+userSelection.SelectedProc+" loaded successfully", w)
}
})

updateProfileList := func() {
var names []string
for _, profile := range userSelection.Profiles {
names = append(names, profile.Name)
}
loadProfileSelect.Options = names
}

profileForm := &widget.Form{
Items: []*widget.FormItem{
{Text: "Profile Name", Widget: profileName},
{Text: "Load Profile", Widget: loadProfileSelect},
},
OnSubmit: func() {
if profileName.Text != "" {
err := userSelection.SaveProfile(profileName.Text)
if err != nil {
dialog.ShowError(err, w)
} else {
dialog.ShowInformation("Success", "Profile saved", w)
updateProfileList()
profileName.SetText("") // Clear the profile name entry after saving
}
} else {
dialog.ShowInformation("Error", "Please enter a profile name", w)
}
},
OnCancel: func() {
if loadProfileSelect.Selected == "" {
dialog.ShowInformation("Error", "Please select a profile to delete", w)
return
}
dialog.NewConfirm(
"Delete Profile",
"Are you sure you want to delete the profile '"+loadProfileSelect.Selected+"'?",
func(confirm bool) {
if confirm {
err := userSelection.DeleteProfile(loadProfileSelect.Selected)
if err != nil {
dialog.ShowError(err, w)
} else {
dialog.ShowInformation("Success", "Profile deleted", w)
updateProfileList()
loadProfileSelect.SetSelected("")
}
}
},
w,
).Show()
},
SubmitText: "Save Profile",
CancelText: "Delete Profile",
}

updateProfileList()

profileTab := container.NewVBox(
widget.NewLabelWithStyle("Profile Management", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
profileForm,
)
initProfiles(w)

initSettings()

Expand Down
Loading

0 comments on commit 705c9b6

Please sign in to comment.