Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows fix for Documents folder not being in the default location. #51

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions client/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,23 @@ def timeSince(epoch:int):
else:
break
return 'Last online %s %s%s ago' % (int(offset), unit, ('' if int(offset) == 1 else 's'))
settingsFile = os.path.expanduser('~/Documents/NSO-RPC/settings.txt')
def getAppPath():
applicationPath = os.path.expanduser('~/Documents/NSO-RPC')
# Windows allows you to move your UserProfile subfolders, Such as Documents, Videos, Music etc.
# However os.path.expanduser does not actually check and assumes its in the default location.
# This tries to correctly resolve the Documents path and fallbacks to default if it fails.
if os.name == 'nt':
try:
import ctypes.wintypes
CSIDL_PERSONAL = 5 # My Documents
SHGFP_TYPE_CURRENT = 0 # Get current, not default value
buf=ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
ctypes.windll.shell32.SHGetFolderPathW(None, CSIDL_PERSONAL, None, SHGFP_TYPE_CURRENT, buf)
applicationPath = os.path.join(buf.value,'NSO-RPC')
except:pass
return applicationPath
applicationPath = getAppPath()
settingsFile = os.path.join(applicationPath,'settings.txt')
settings = {
'dark': False,
}
Expand Down Expand Up @@ -387,7 +403,7 @@ def updateProfile(self, new):
client.api.targetID = userSelected
else:
client.api.targetID = None
with open(os.path.join(os.path.expanduser('~/Documents/NSO-RPC'), 'private.txt'), 'w') as file:
with open(os.path.join(applicationPath, 'private.txt'), 'w') as file:
file.write(json.dumps({
'session_token': client.api.session_token,
'user_lang': client.api.user_lang,
Expand Down
30 changes: 23 additions & 7 deletions client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
import threading
from api import *

def getAppPath():
applicationPath = os.path.expanduser('~/Documents/NSO-RPC')
# Windows allows you to move your UserProfile subfolders, Such as Documents, Videos, Music etc.
# However os.path.expanduser does not actually check and assumes its in the default location.
# This tries to correctly resolve the Documents path and fallbacks to default if it fails.
if os.name == 'nt':
try:
import ctypes.wintypes
CSIDL_PERSONAL = 5 # My Documents
SHGFP_TYPE_CURRENT = 0 # Get current, not default value
buf=ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
ctypes.windll.shell32.SHGetFolderPathW(None, CSIDL_PERSONAL, None, SHGFP_TYPE_CURRENT, buf)
applicationPath = os.path.join(buf.value,'NSO-RPC')
except:pass
return applicationPath
applicationPath = getAppPath()

class Discord():
def __init__(self, session_token = None, user_lang = None, rpc = False, targetID = None, version = None):
self.rpc = None
Expand Down Expand Up @@ -72,7 +89,7 @@ def update(self):
self.api.targetID = input('Which user are you?\n-----\n%s\n-----\nPlease enter the ID here: ' % '\n-----\n'.join([ '%s (%s)' % (x.name, x.nsaId) for x in client.api.friends]))
if not self.api.targetID in (x.nsaId for x in client.api.friends):
sys.exit(log('Unknown ID input by user'))
with open(os.path.join(os.path.expanduser('~/Documents/NSO-RPC'), 'private.txt'), 'w') as file:
with open(os.path.join(applicationPath, 'private.txt'), 'w') as file:
file.write(json.dumps({
'session_token': self.api.session_token,
'user_lang': self.api.user_lang,
Expand Down Expand Up @@ -123,15 +140,14 @@ def background(self):
time.sleep(1)

def logout(self):
path = os.path.expanduser('~/Documents/NSO-RPC')
if os.path.isfile(os.path.join(path, 'private.txt')):
try:os.remove(os.path.join(path, 'private.txt'))
if os.path.isfile(os.path.join(applicationPath, 'private.txt')):
try:os.remove(os.path.join(applicationPath, 'private.txt'))
except:pass
try:os.remove(os.path.join(path, 'settings.txt'))
try:os.remove(os.path.join(applicationPath, 'settings.txt'))
except:pass
sys.exit()

def getToken(manual = True, path:str = os.path.expanduser('~/Documents/NSO-RPC/private.txt')):
def getToken(manual = True, path:str = os.path.join(applicationPath, 'private.txt')):
session_token, user_lang, targetID = None, None, None
if os.path.isfile(path):
with open(path, 'r') as file:
Expand All @@ -151,7 +167,7 @@ def getToken(manual = True, path:str = os.path.expanduser('~/Documents/NSO-RPC/p
user_lang = input('Please enter your language from the list below:\n%s\n> ' % ('\n'.join(languages)))
if not user_lang in languages:
raise Exception('invalid user language')
tempToken = os.path.expanduser('~/Documents/NSO-RPC/tempToken.txt')
tempToken = os.path.join(applicationPath, 'tempToken.txt')
if not os.path.isfile(path) and os.path.isfile(tempToken):
os.remove(tempToken)
return session_token, user_lang, targetID
Expand Down